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
相关文章推荐
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 红宝石(Ruby)史话
- IE:获取完整的网页
- IE:脱机浏览网页
- 网页恶意代码的预防
- Ruby简明教程之循环语句介绍
- ruby 一些简单的例子
- Ruby 之 class 中的 private、 protected、public
- Ruby中操作文件的方法介绍
- Ruby中的集合编写指南
- CentOS 6.3下编译安装Ruby 2.0笔记
- Ruby编程中关于中断和返回的用法教程
- Ruby中的异常处理代码编写示例
- Ruby中的变量学习总结
- 几个加速Ruby on Rails的编程技巧
- Ruby配置rspec和RestClient来检测服务器
- 分析Cache 在 Ruby China 里面的应用情况
- Ruby中数组的一些相关使用方法