1

我写了下面的代码,只是为了启动 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>

代码

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'))

为什么会这样以及如何解决相同的问题,请告诉我。

4

1 回答 1

4

看来您需要配置代理。找出您的组织的代理 URL/端口是什么(以及是否需要进行身份验证)。您可以从浏览器配置中查看此信息。为了在您的 Ruby 代码中使用它,您需要设置HTTP_PROXY环境变量。

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

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

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

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

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

于 2013-01-22T16:20:00.320 回答