0%

ruby使用nokogiri抓取网页

nokogiri这个gem实在好用,配合spidr ,可以很方便的抓取网页 或图片。

spidr使用nokogiri

所以要灵活的话还是要用nokogiri 。

 

require ‘net/http’
require “open-uri”
require ‘nokogiri’

weburl=’http://slide.eladies.sina.com.cn/fa/slide_3_22147_9430.html#p=17
doc = Nokogiri::HTML.parse(open(weburl), nil, ‘gb2312’) #不这样写会有乱码
doc.css(‘dl dd’).each do |link|

puts link.content

rescue
puts ‘error’
end
学习教程:

http://ruby.bastardsbook.com/chapters/html-parsing/