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

人走茶凉 提交于 2019-12-06 10:48:16

You should use open-uri and set the user agent as parameter in open function .

Below is an example where I am setting user Agent in a variable and using that as parameter in open function .

    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 = "http://www.somedomain.com/somepage/"

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

There is an option to set readtime out in openURI

You can review the documentation of Open URI in the below link

Open URI documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!