Created
February 5, 2011 15:23
-
-
Save satococoa/812521 to your computer and use it in GitHub Desktop.
画像ファイルダウンローダ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'nokogiri' | |
| require 'open-uri' | |
| require 'pathname' | |
| url = ARGV[0] | |
| dir = Pathname(ARGV[1]) | |
| unless dir.exist? | |
| Dir::mkdir dir | |
| end | |
| doc = Nokogiri::HTML(open(url).read) | |
| retrycount = 0 | |
| doc.css('a').each do |link| | |
| if link['href'] =~ /\.(jpg|png|gif)$/ | |
| img = link['href'] | |
| dest = dir+File::basename(img) | |
| puts "downloading #{img} to #{dest} ..." | |
| begin | |
| open(dest, 'wb'){ |f| | |
| f.puts open(img).read | |
| } | |
| retrycount = 0 | |
| rescue Timeout::Error | |
| retrycount += 1 | |
| if retrycount < 3 | |
| puts "retrying ..." | |
| retry | |
| end | |
| end | |
| end | |
| end | |
| puts 'mission complete!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment