对于Nokogiri,我将会得到错误“初始化”:getaddrinfo:没有这样的主机是已知的。(SocketError)”

时间:2022-10-05 20:39:44

I worte the below code, just to start the Nokogiri to mee the requirement and when I ran it for testing got the error:

我用下面的代码,只是为了开始Nokogiri对mee的要求,当我运行它进行测试时得到了错误:

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

Why so and how to fix the same,please advice me.

为什么这么做,怎么解决,请给我建议。

1 个解决方案

#1


4  

It appears you need to configure a proxy. Find out what the proxy URL/Port is for your organization (and whether there needs to be authentication). You may be able to view this information from your browser configuration. In order to use it with your Ruby code, you need to set the HTTP_PROXY environment variable.

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

You can set it in Ruby code:

你可以用Ruby代码来设置:

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

or if you need authentication:

或者你需要认证:

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

A more permanent solution is to set HTTP_PROXY in your system environment variables.

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

#1


4  

It appears you need to configure a proxy. Find out what the proxy URL/Port is for your organization (and whether there needs to be authentication). You may be able to view this information from your browser configuration. In order to use it with your Ruby code, you need to set the HTTP_PROXY environment variable.

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

You can set it in Ruby code:

你可以用Ruby代码来设置:

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

or if you need authentication:

或者你需要认证:

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

A more permanent solution is to set HTTP_PROXY in your system environment variables.

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