问题
>>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