require 'rubygems' require 'hpricot' require 'open-uri' require 'net/http' Net::HTTP.version_1_2 links_list = [] n = 1 #画像のuriを収集 for i in 0..50 begin doc = Hpricot(open(uri = "http://doragong.19.dtiblog.com/page-#{i}.html").read.downcase) rescue sleep(1) retry end (doc/:a).each {|link| if link[:href] =~ /\.jpe?g/ links_list << link[:href] end } end #画像をダウンロードして保存 links_list.uniq!.each{|link| if link =~ %r|http://(.*?)(/.*/(.*\.jpg))| print n,"\sDownloding...\s",link,"\n" begin open($3,"wb"){|file| Net::HTTP.start($1, 80) {|http| response = http.get($2) file.puts response.body } } rescue next end n += 1 end } |
コメント