Ruby – 如果url是重定向,如何下载文件?

如果url是重定向,Ruby如何下载文件?

我正在尝试下载此url:

soundcloud.com/stereo-f—/cohete-amigo/download

重定向是这样的:

[ec-media.soundcloud.com/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&Expires=1352919869&Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D][2]

我试过的是这个:

Net::HTTP.start("ec-media.soundcloud.com") { |http| resp = http.get("/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&Expires=1352919869&Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D") open("test.wav", "wb") { |file| file.write(resp.body) } } puts "Done." sleep 50 

我不明白/知道任何互联网协议和重定向和那些东西……请给我一个很好的解释或帮助我的脚本从rubcl下载soundcloud的文件?

谢谢

更新

我试过这种方式,但我收到一个错误:

 Net::HTTP.start("soundcloud.com") do |http| resp = http.get("/dubstep-4/kill-paris-tender-love/download") while resp.code == '301' || resp.code == '302' # moved permanently or temporarily: try again at the new location. resp = http.get(URI.parse(resp.header['location'])) # ideally you should also bail if you make too many redirects. end # make sure the request was successful. if resp.code == '200' open("test.wav", "wb"){ |file| file.write(resp.body) } else puts "Error: HTTP #{resp.code}" end end 

错误:

 C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1860:in `initialize': undefined method `empty?' for # (NoMethodError) from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:2093:in `initialize' from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1026:in `new' from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1026:in `get' from C:/Users/Administrador/Desktop/1.rb:59:in `block in ' from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:745:in `start' from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:557:in `start' from C:/Users/Administrador/Desktop/1.rb:48:in `' 

有一个名为Open URI的标准库,它比Net HTTP更高级别。 它会自动重定向:

 require 'open-uri' file = open("http://soundcloud.com/stereo-f---/cohete-amigo/download") 

你需要做的是查看resp.code并处理重定向:

 Net::HTTP.start("your.web.site") do |http| resp = http.get("/something") while resp.code == '301' || resp.code == '302' # moved permanently or temporarily: try again at the new location. resp = http.get(URI.parse(resp.header['location'])) # ideally you should also bail if you make too many redirects. end # make sure the request was successful. if resp.code == '200' open("test.wav", "wb"){ |file| file.write(resp.body) } else puts "Error: HTTP #{resp.code}" end end 

我建议你在这里看到这个指南,但要点是你必须检查响应的类型,然后在重定向的情况下从正文中提取URL。

现在,SoundCloud可能在后台继续使用cookie和临时URL等,但这是另一个需要处理的问题。