Ruby 批量下载图片文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值