Nokogiri代理和用户代理出错

时间:2017-06-14 06:04:29

标签: ruby proxy user-agent

我从中获取代码,因为我需要获得美国代理和良好用户代理的内容。

How to GET a URL with User-Agent and timeout through some Proxy in Ruby?

require 'rubygems'
require 'nokogiri'
require 'open-uri'

    user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.854.0 Safari/535.2"

    url = "google.com"

    @doc = Nokogiri::HTML(open(url, 'User-Agent' => user_agent, 'read_timeout' => 10 ), nil, "UTF-8")

但我收到了错误:

/usr/lib/ruby/2.3.0/net/http/header.rb:18:in `block in initialize_http_header': undefined method `strip' for 10:Fixnum (NoMethodError)
    from /usr/lib/ruby/2.3.0/net/http/header.rb:16:in `each'
    from /usr/lib/ruby/2.3.0/net/http/header.rb:16:in `initialize_http_header'
    from /usr/lib/ruby/2.3.0/net/http/generic_request.rb:44:in `initialize'
    from /usr/lib/ruby/2.3.0/net/http/request.rb:15:in `initialize'
    from /usr/lib/ruby/2.3.0/open-uri.rb:320:in `new'
    from /usr/lib/ruby/2.3.0/open-uri.rb:320:in `block in open_http'
    from /usr/lib/ruby/2.3.0/net/http.rb:853:in `start'
    from /usr/lib/ruby/2.3.0/open-uri.rb:319:in `open_http'
    from /usr/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open'
    from /usr/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop'
    from /usr/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
    from /usr/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
    from /usr/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
    from /usr/lib/ruby/2.3.0/open-uri.rb:717:in `open'
    from /usr/lib/ruby/2.3.0/open-uri.rb:35:in `open'
    from work6.rb:10:in `<main>'

0 个答案:

没有答案