Ruby-如果URL是重定向的,如何下载文件? [英] Ruby - how to download a file if the url is a redirection?

查看:86
本文介绍了Ruby-如果URL是重定向的,如何下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ruby how to download a file if the url is a redirection?

我正在尝试下载此网址:

i'm trying to download this url:

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

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

重定向是这样的:

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

[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

我不了解/不知道任何互联网协议和重定向,而且这些东西...请给我一个很好的解释或帮助我的脚本,以便使用ruby从soundcloud下载文件.

i don't understand/know ANY of internet protocols and redirections and that things... please can give me a good explanation or help with my script to download files from soundcloud with ruby?

谢谢

更新:

我已经尝试过这种方法,但是出现错误:

I've tried this way but i get an error:

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 #<URI::HTTP:0x25a9ce8> (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 <main>'
        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 `<main>'

推荐答案

有一个称为Open URI的标准库,它比Net HTTP高.它会自动重定向:

There is a standard library called Open URI that is higher level than Net HTTP. It follows redirects automatically:

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

这篇关于Ruby-如果URL是重定向的,如何下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆