Get HTML source of a https page by forcing a user agent in Ruby

戏子无情 提交于 2019-12-24 14:10:34

问题


>>require 'net/https'
>>uri = URI('https://www.facebook.com/careers/department?dept=product-management&req=a2KA0000000E147MAC')
>>conn = Net::HTTP.new(uri.host, uri.port)
>>req = Net::HTTP::Get.new(uri.request_uri, {'User Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1'})
>>resp = conn.request req
=> #<Net::HTTPFound 302 Found readbody=true>

The 302 redirection thrown by the website redirects to a 'unsupported browser' page. What am I doing wrong in setting the user agent for this request? I'm using the same user agent string returned by browser. Additional info: I cannot use libraries such as watir in this use case. Any solution by using either 'net/http[s]' or 'open-uri' would be awesome.


回答1:


Change 'User Agent' to 'User-Agent' with a hyphen.



来源:https://stackoverflow.com/questions/12239367/get-html-source-of-a-https-page-by-forcing-a-user-agent-in-ruby

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