您的位置:首页 > 编程语言 > Ruby

Ruby 批量下载图片文件

2015-08-28 17:34 417 查看
system("title PicDownloader")
require 'open-uri'
@fail=[] # The list of URLs failed to access
@num = 0 # Total number of files to be downloaded
@tsz = 0 # Total size of files to be downloaded
def down(uri,n)
begin
data = open(uri, 'User-Agent' => 'ruby'){|f| f.read}
file = File.new("./6#{n}/#{File.basename(uri)}", 'w+')
file.binmode
file << data
file.flush
file.close
@num += 1
@tsz += data.size
puts "File:#{File.basename(uri)};Size:#{data.size} Bytes;100%"
rescue Errno
puts "File:#{File.basename(uri)};Failed;Error:#{$!} *"
@fail.push([uri,n,$!])
rescue
puts "File:#{File.basename(uri)};Failed;Error:#{$!} *"
@fail.push([uri,n,$!])
end
end

for i in "01".."13"
Dir.mkdir("6#{i}") unless FileTest.exist?("6#{i}") # Make a new directory
for j in "01".."60"
# Try to connect, if failed then jump out of this cycle.
begin
open("http://www.hz2hs.net.cn/byweb/2015new/Pages/6#{i}/6#{i}#{j}1.jpg")
rescue
next
end
down("http://www.hz2hs.net.cn/byweb/2015new/Pages/6#{i}/6#{i}#{j}1.jpg",i)
down("http://www.hz2hs.net.cn/byweb/2015new/Pages/6#{i}/6#{i}#{j}2.jpg",i)
down("http://www.hz2hs.net.cn/byweb/2015new/Pages/6#{i}/6#{i}#{j}1big.jpg",i)
end
end
# If failure(s) exist then try to remedy
loop do
@t = 1
print "\nOperated OK. Please wait"
while  @t < 7 ; sleep 1 ; print "." ; @t += 1 ; end
if @fail==[] then break end
puts "\nFailed URL(s) List:"
@fail.each {|i| puts i[0]}
puts "\nRetry?(y/n)"
if gets.downcase != "y\n" then break end
tmp = @fail
@fail = []
tmp.each {|i| down(i[0],i[1])} # Re-download
end
puts "Totally downloaded #{@tsz} bytes in #{@num} files."
unless @fail == []
# Set down the error message into a file
puts "#{@fail.size} file(s) out of #{@num + @fail.size} failed to download. Please refer to \"Err.txt\"."
f = File.open("Err.txt","w")
@fail.each {|k| f.puts k[0],k[2]}
f.close
end

print "This process will be terminated in #{@t-=1} second(s)\r" while sleep(1) && @t > 0
exit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息