ruby simpledesktops_scraper.rb
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby simpledesktops_scraper.rb相关的知识,希望对你有一定的参考价值。
require 'rubygems'
require 'hpricot'
require 'open-uri'
@url = "http://simpledesktops.com/browse/"
def scrape_page(url, page_number=0)
puts "Scraping #{url}"
Dir.mkdir "#{page_number}" unless File.exists? "#{page_number}"
doc = Hpricot( open( url ) )
( doc/".desktop > a" ).each do |link|
href = link.attributes["href"]
path = "#{page_number}/#{File.basename href}"
unless File.exists? path
print "#{href}... "
open( href ) do |image|
File.open( path, "wb" ) do |f|
f.write image.read
puts "Saved #{path}"
end
end
else
puts "#{path} exists"
end
end
next_page = ( doc/".pagination .older" )
unless next_page.length == 0
u = URI.parse url
next_page_url = URI::HTTP.build({ :host => u.host, :path => next_page[0].attributes["href"] }).to_s
puts "Next page: #{next_page_url}"
page_number += 1
scrape_page next_page_url, page_number
end
end
scrape_page @url
以上是关于ruby simpledesktops_scraper.rb的主要内容,如果未能解决你的问题,请参考以下文章
Ruby运算符
Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速
如何学习ruby?Ruby学习技巧分享
ruby Ruby脚本,看看是否用openssl编译了ruby
什么是ruby?
ruby和ruby ee