如何使用Ruby将网页的HTML源代码加载到字符串中,然后解析它以获取<title>标记的内部HTML?</title>

时间:2014-11-09 00:21:02

标签: html ruby

我试图使用Ruby在HTML源代码字符串中获取标记的内部HTML。我怎样才能做到这一点?我搜索过并搜索过,目前还没有任何解决方案对我有用。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

看看这个例子

希望它有所帮助

require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML::Document for the page we’re interested in...

doc = Nokogiri::HTML(open('http://www.google.com/search?q=sparklemotion'))

# Do funky things with it using Nokogiri::XML::Node methods...

####
# Search for nodes by css
doc.css('h3.r a').each do |link|
  puts link.content
end

####
# Search for nodes by xpath
doc.xpath('//h3/a').each do |link|
  puts link.content
end

####
# Or mix and match.
doc.search('h3.r a.l', '//h3/a').each do |link|
  puts link.content
end