有没有啥爬虫案例可供参考的啊?,我想写写爬虫,目前不知道如何下手
Ruby 的确实不多,推荐一个给你,觉得不错,有实例,讲的比较全面,而且易懂: http://ruby.bastardsbook.com/chapters/web-scraping/
之前我也准备问论坛的呢,如果大家能多贡献一些的话,楼主可以总结一下,造福后来人啊
打个酱油。
require "mechanize"
# ruby aichen.rb pagenmber
url="http://www.aisex.com/bt/thread.php?fid=14&page="
page_num=ARGV[0]
agent=Mechanize.new
agent.user_agent_alias = 'Windows IE 9'
file="pics/"
page=agent.get(url+page_num)
file=file+page_num+"/"
page.links_with(:text => /\[\d+P\]/).each do |link|
puts link.href
imgcount=0
next_page=link.click
subfile=next_page.at('h1#subject_tpc').content
puts subfile
next_page.images_with(:src => /jpg/).each do |img|
puts img.url
begin
img.fetch.save(file+subfile+"/"+imgcount.to_s+".jpg")
rescue
puts "can not get this one"
end
imgcount+=1
end
end