Python 中的 cookielib 和表单身份验证问题
InstaMapper 是一种 GPS 跟踪服务,当设备在 InstaMapper 网页上实时跟踪时,它会更频繁地更新设备的位置。我希望这种情况一直发生,所以我想我应该编写一个 python 脚本来登录我的帐户并定期访问该页面。
import urllib2, urllib, cookielib
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
params = urllib.urlencode(dict(username_hb='user', password_hb='hunter2'))
opener.open('http://www.instamapper.com/fe?action=login', params)
if not 'id' in [cookie.name for cookie in cj]:
raise ValueError, "Login failed"
# try secured page
resp = opener.open('http://www.instamapper.com/fe?page=track&device_key=abc')
print resp.read()
resp.close()
每次都会引发 ValueError。如果我删除它并阅读响应,该页面会认为我已禁用 cookie 并阻止对该页面的访问。为什么 cj 不抓取 InstaMapper cookie?
有没有更好的方法让跟踪服务认为我正在不断查看我的帐户?
InstaMapper is a GPS tracking service that updates the device's position more frequently when the device is being tracked live on the InstaMapper webpage. I'd like to have this happen all the time so I thought I'd write a python script to login to my account and access the page periodically.
import urllib2, urllib, cookielib
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
params = urllib.urlencode(dict(username_hb='user', password_hb='hunter2'))
opener.open('http://www.instamapper.com/fe?action=login', params)
if not 'id' in [cookie.name for cookie in cj]:
raise ValueError, "Login failed"
# try secured page
resp = opener.open('http://www.instamapper.com/fe?page=track&device_key=abc')
print resp.read()
resp.close()
The ValueError is raised each time. If I remove this and read the response, the page thinks I have disabled cookies and blocks access to that page. Why isn't cj grabbing the InstaMapper cookie?
Are there better ways to make the tracking service think I'm viewing my account constantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
action=login
是参数的一部分,应进行相应处理:(此外,我认为这个特定的用户名/密码组合无效,您实际上在实际代码中使用了有效的用户名和密码,否则登录失败。)
action=login
is part of the parameters, and should be treated accordingly:(Also, this particular username/password combination is invalid, I assume, that you actually use a valid username and password in your actual code, otherwise the login fails corretly.)
您是否查看过是否有专门设计的 cookie 来挫败您的尝试?我建议使用 Wireshark 或其他检查器来查看当您手动登录时是否有 cookie 发生变化(通过 javascript 等)。
(道德说明:您可能违反了服务条款,并给公司带来了比您更多的成本我曾经运行过这样的服务,每次额外/计划外的位置更新都在 0.01 美元 - 0.05 美元之间,但我确信它会下降。)
Have you looked at whether there is a cookie specifically designed to foil your attempts? I suggest using Wireshark or other inspector to see if there is a cookie that changes (via javascript, etc) when you manually log in.
(Ethical note: You may be violating the terms of service and incurring much more cost to the company than you are paying for. I used to run a service like this and every additional/unplanned location update was between $0.01 - $0.05 but I'm sure its come down.)