我正在学习编程,所以我一直在想一些随机的项目。我决定制作一个脚本来下载我在过去几年中收集的所有 GIF。一切似乎都在工作,除了我在中途收到一个错误,我的错误处理没有捕获(无效的访问模式“wb”,“ArgumentError URI:HTTP 资源是只读的)。我已经摆弄了一天多无法获得这项工作任何帮助或提示将不胜感激。
require 'json', 'open-uri'
def download
file = open("C:/sites/bookmark/gifs.json")
json = file.read
parsed = JSON.parse(json)
gifs = parsed["children"]
file.close()
total = 0
gifsize = 0
gifs.each do |key|
source = "#{key["uri"]}"
source.chomp
gifname = "#{key["title"]}"
gifname.gsub(/[^0-9a-z ]/i, '')
open("#{gifname}.gif", "wb") do |file|
begin
open(source)
rescue Exception => e
case e.message
when /404/ then puts '404!'
when /505/ then puts '505!'
when /408/ then puts '408!'
else puts 'idk #{e.message}'
end
file.close()
File.delete("#{gifname}.gif")
puts "Deleted #{key["title"]}.gig"
next
end
open(source) do |uri|
file.write(uri.read)
total += 1
gifsize += (uri.size / 1024)
print "#{(uri.size)/1024} KiloBytes"
end
print " --- #{total}"
end
puts " next gif---"
end
puts "\n#{gifsize} Total KB Downloaded \n#{total} Total Gifs "
end
download()
谢谢