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=tenderlove'))
# Do funky things with it using Nokogiri::XML::Node methods...
####
# Search for nodes by css
doc.css('h3.r a.l').each do |link|
puts link.content
end
####
# Search for nodes by xpath
doc.xpath('//h3/a[@class="l"]').each do |link|
puts link.content
end
####
# Or mix and match.
doc.search('h3.r a.l', '//h3/a[@class="l"]').each do |link|
puts link.content
end
执行以上代码报如下错误:
no such file to load -- nokogiri (LoadError)
第一行加入下面代码即可:
require 'rubygems'
本文介绍如何利用Ruby的Nokogiri库抓取网页内容,通过示例展示了如何解析Google搜索结果页面,并获取特定节点的数据。文章中解决了一个加载Nokogiri库时出现的常见错误。
442

被折叠的 条评论
为什么被折叠?



