python urllib2 不返回 https 页面
当我尝试将数据从 http 发布到 https 时,urllib2 不会返回所需的 https 网页,而是网站要求启用 cookie。
要获取第一个 http 页面:
proxyHandler = urllib2.ProxyHandler({'http': "http://proxy:port" })
opener = urllib2.build_opener(proxyHandler)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')]
urllib2.install_opener(opener)
resp = urllib2.urlopen(url)
content = resp.read()
当我从上面的页面提取数据并将数据发布到第二个 https 页面时,urllib2 返回成功状态 200 并且页面要求启用 cookie。
我检查了帖子数据,没问题。我从网站获取 cookie,但不确定它们是否与下一个请求一起发送,因为我在 python 文档中读到 urllib2 自动处理 cookie。
要获取第二个 https 页面:
resp = urllib2.urlopen(url, data=postData)
content = resp.read()
我还尝试将代理处理程序设置为此,正如在 stackoverflow 上某处对类似问题的回复中所读取的那样,但得到了相同的结果:
proxyHandler = urllib2.ProxyHandler({'https': "http://proxy:port" })
When I try to post data from http to https, urllib2 does not return desired https webpage instead website asks to enable cookies.
To get first http page:
proxyHandler = urllib2.ProxyHandler({'http': "http://proxy:port" })
opener = urllib2.build_opener(proxyHandler)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')]
urllib2.install_opener(opener)
resp = urllib2.urlopen(url)
content = resp.read()
When I extract data from above page and post data to second https page, urllib2 returns success status 200 and page asks to enable cookies.
I've checked the post data, its fine. I'm getting cookies from website but not sure whether they are being sent with next request or not as I read in python docs that urllib2 automatically handles cookies.
To get second https page:
resp = urllib2.urlopen(url, data=postData)
content = resp.read()
I also tried to set proxy handler to this as read in a reply to similar problem on stackoverflow somewhere but got same result:
proxyHandler = urllib2.ProxyHandler({'https': "http://proxy:port" })
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
urllib2 “处理”响应中的 cookie,但它不会自动存储它们并在以后的请求中重新发送它们。为此,您需要使用
cooklib
模块。文档中有一些示例,展示了它如何与 urllib2 一起使用。
urllib2
"handles" cookies in responses but it doesn't not automatically store them and resend them with later requests. You'll need to use thecooklib
module for that.There are some examples in the documentation that show how it works with
urllib2
.