Ruby 批量下载图片文件
来源:互联网 发布:网络词扑街是什么意思 编辑:程序博客网 时间:2024/05/17 08:12
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 downloadeddef down(uri,n)begindata = open(uri, 'User-Agent' => 'ruby'){|f| f.read}file = File.new("./6#{n}/#{File.basename(uri)}", 'w+')file.binmodefile << datafile.flushfile.close@num += 1@tsz += data.sizeputs "File:#{File.basename(uri)};Size:#{data.size} Bytes;100%"rescue Errnoputs "File:#{File.basename(uri)};Failed;Error:#{$!} *"@fail.push([uri,n,$!])rescueputs "File:#{File.basename(uri)};Failed;Error:#{$!} *"@fail.push([uri,n,$!])endendfor i in "01".."13"Dir.mkdir("6#{i}") unless FileTest.exist?("6#{i}") # Make a new directoryfor j in "01".."60"# Try to connect, if failed then jump out of this cycle.beginopen("http://www.hz2hs.net.cn/byweb/2015new/Pages/6#{i}/6#{i}#{j}1.jpg")rescuenextenddown("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)endend# If failure(s) exist then try to remedyloop do@t = 1print "\nOperated OK. Please wait"while @t < 7 ; sleep 1 ; print "." ; @t += 1 ; endif @fail==[] then break endputs "\nFailed URL(s) List:"@fail.each {|i| puts i[0]}puts "\nRetry?(y/n)"if gets.downcase != "y\n" then break endtmp = @fail@fail = []tmp.each {|i| down(i[0],i[1])} # Re-downloadendputs "Totally downloaded #{@tsz} bytes in #{@num} files."unless @fail == []# Set down the error message into a fileputs "#{@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.closeendprint "This process will be terminated in #{@t-=1} second(s)\r" while sleep(1) && @t > 0exit
0 0
- Ruby 批量下载图片文件
- java 批量下载图片,批量打包文件并下载
- java 批量下载图片,批量打包文件并下载
- 通过PHP批量下载图片文件
- 批量下载css文件中的图片
- PHP批量下载图片文件(抓取css中图片)
- Python批量下载图片
- 批量下载大量图片
- php 图片批量下载
- 怎么批量下载图片
- node批量下载图片
- python批量下载图片
- python 批量下载图片
- Python3 批量下载图片
- Python批量下载图片
- 网页批量下载图片
- python下载批量图片
- python批量下载图片
- unity 在game视图模型穿插问题
- user,debug,userdebug版本
- ORB特征点检测
- Apache Http Server和Tomcat 之区别
- executeQuery()
- Ruby 批量下载图片文件
- 张逸:ThoughtWorks(中国)程序员读书雷达
- jQuery ajax - ajax() 方法
- Komodo for mac 破解版
- typedef函数指针用法
- 三角函数逼近锯齿函数和阶梯函数
- Python字符串的encode与decode研究心得乱码问题解决方法
- 集合、Collection、List的总体概述
- 事件分发、拦截、消费(一)