使用Nokogiri我收到错误“initialize”:getaddrinfo:没有这样的主机是已知的。(SocketError)“

时间:2013-01-22 12:51:58

标签: ruby nokogiri

我打算下面的代码,只是为了启动Nokogiri来满足要求,当我运行它进行测试时得到了错误:

D:\WIPData\Ruby\Scripts>Nokogiri.rb
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `initialize': getaddrinfo: No such
host is known.  (SocketError)
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `open'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
        from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
        from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `connect'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:744:in `start'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:775:in `buffer_open'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:677:in `open'
        from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:33:in `open'
        from D:/WIPData/Ruby/Scripts/Nokogiri.rb:6:in `<main>'

D:\WIPData\Ruby\Scripts>

CODE

require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML::Document for the page we’re interested in...

doc = Nokogiri::HTML(open('http://www.google.co.in'))

为什么如此以及如何解决这个问题,请告诉我。

1 个答案:

答案 0 :(得分:4)

您似乎需要配置代理。找出您的组织的代理URL /端口(以及是否需要身份验证)。您可以从浏览器配置中查看此信息。要将它与Ruby代码一起使用,您需要设置HTTP_PROXY环境变量。

您可以在Ruby代码中设置它:

ENV['HTTP_PROXY'] = 'http://hostname:port'

或者如果您需要身份验证:

ENV['HTTP_PROXY'] = 'http://username:password@hostname:port'

更持久的解决方案是在系统环境变量中设置HTTP_PROXY