【问题标题】:Download list of files loop - net/http下载文件循环列表 - net/http
【发布时间】:2012-05-16 11:52:10
【问题描述】:
 require 'net/http' 
 1.upto(99) do |novel|
    puts "Downloading ##{novel}..."
    Net::HTTP.start("http://www.nbc.com") do |http|
      resp = http.get("/heroes/novels/downloads/Heroes_novel_0#{novel}.pdf")
      open("Heroes_novel_#{novel}.pdf", "w") do |file|
        file.write(resp.body)
      end
    end
    puts "Next..."
    puts
 end
puts "Okay, Sneak it's done!"

运行脚本时出现以下错误:

C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:in initialize': getaddrinfo: No such host is known. (SocketError) from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:inopen' 来自 C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:in block in connect' from C:/Ruby192/lib/ruby/1.9.1/timeout.rb:44:intimeout' 来自 C:/Ruby192/lib/ruby/1.9.1/timeout.rb:89:in timeout' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:inconnect' 来自 C:/Ruby192/lib/ruby/1.9.1/net/http.rb:637:in do_start' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:626:instart' 来自 C:/Ruby192/lib/ruby/1.9.1/net/http.rb:490:in start' from heroes.rb:5:inblock in ' 来自 heros.rb:3:in upto' from heroes.rb:3:in'

【问题讨论】:

    标签: ruby loops download


    【解决方案1】:

    您需要将正确的参数解析为start()。它需要一个主机和一个端口。最简单的方法是先创建一个 URI 对象。

    uri = URI('http://www.nbc.com')
    Net::HTTP.start(uri.host, uri.port) do |http|
      #do some get requests and handle it
    end
    

    这转换为Net::HTTP.start("www.nbc.com", 80)

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多