Python CookieJar 保存 cookie,但不将其发送到网站
我正在尝试使用 urllib2 和 cookiejar 登录网站。它保存了会话 ID,但是当我尝试打开另一个需要身份验证的链接时,它说我没有登录。我做错了什么?
这是代码,对我来说失败了:
import urllib
import urllib2
import cookielib
cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
# Gives response saying that I logged in succesfully
response = opener.open("http://site.com/login", "username=testuser&password=" + md5encode("testpassword"))
# Gives response saying that I am not logged in
response1 = opener.open("http://site.com/check")
I am trying to login to website using urllib2 and cookiejar. It saves the session id, but when I try to open another link, which requires authentication it says that I am not logged in. What am I doing wrong?
Here's the code, which fails for me:
import urllib
import urllib2
import cookielib
cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
# Gives response saying that I logged in succesfully
response = opener.open("http://site.com/login", "username=testuser&password=" + md5encode("testpassword"))
# Gives response saying that I am not logged in
response1 = opener.open("http://site.com/check")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的实现看起来不错......并且应该可以工作。
它应该发送正确的
cookies
,但我认为网站实际上没有让您登录。你怎么能说它没有发送
cookie
或者您收到的cookie
可能不是对您进行身份验证的。使用:
response.info()
查看响应的标头,以了解您实际收到的 cookie。该网站可能不会让您登录,因为:
它会检查您未设置的
用户代理
,因为某些网站从 4 个主要浏览器打开只是为了禁止机器人访问。该网站可能正在寻找一些您可能不会发送的特殊隐藏表单字段。
1 条建议:
此外,这里有一件奇怪的事情:
site.com
在 JavaScript 中实现 md5 时才可能实现。查看.. !!
:)
Your implementation seems fine... and should work.
It should be sending in the correct
cookies
, but I see it as the case when the site is actually not logging you in.How can you say that its not sending the
cookies
or may becookies
that you are getting are not the one that authenticates you.Use :
response.info()
to see the headers of the responses to see what cookies you are receiving actually.The site may not be logging you in because :
Its having a check on
User-agent
that you are not setting, since some sites open from 4 major browsers only to disallow bot access.The site might be looking for some special hidden form field that you might not be sending in.
1 piece of advise:
Moreover 1 thing is strange here :
site.com
implements md5 in javascript.Check out.. !!
:)
我自己的测试服务器也遇到了类似的问题,该服务器在浏览器中运行良好,但在
urllib2.build_opener
解决方案中却不行。问题似乎出在 urllib2 上。正如这些答案所建议的,使用更强大的mechanize 库而不是 urllib2:
并且开启器将按预期工作!
I had a similar problem with my own test server, which worked fine with a browser, but not with the
urllib2.build_opener
solution.The problem seems to be in urllib2. As these answers suggest, it's easy to use more powerful mechanize library instead of urllib2:
And the opener will work as expected!