通过强制Ruby中的用户代理获取https页面的HTML源代码 [英] Get HTML source of a https page by forcing a user agent in Ruby

查看:248
本文介绍了通过强制Ruby中的用户代理获取https页面的HTML源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

网站引发的302重定向重定向到不受支持的浏览器页面。在为此请求设置用户代理时,我做错了什么?我使用浏览器返回的相同的用户代理字符串。
附加信息:我不能在这个用例中使用像watir这样的库。通过使用'net / http [s]'或'open-uri'的任何解决方案都会很棒。

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.

推荐答案

用户代理到用户代理,连字符。

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

这篇关于通过强制Ruby中的用户代理获取https页面的HTML源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆