我尝试使用Python登录网站并保存cookies.txt文件,但它不起作用。但我觉得我非常接近!
在我运行Python脚本之后,cookies.txt文件看起来像这样:
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
website.com FALSE / FALSE PHPSESSID ejFRMWtNc0YwYjdVeXQ4c25NWUUvOC9CcVZXUnV4eFhjNnlITGs...
当我登录同一网站并从Firefox手动导出时,它看起来像这样:
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
website.com FALSE / FALSE 0 SRLOGIN OfHFowieHRHizaoriK
website.com FALSE / FALSE 0 PHPSESSID L2tCdzNoZnhXQ09DV0FZUk4zc1pxSXhJS2ZvRDNjeTNlUU9WQlVvOGlRNW9mSXdGTFZyd...
所以区别在于后一个文件有两行,而#39; 0',我相信这是指cookie的超时(因此设置为不会过期)。
这是我用来创建cookies.txt文件的Python脚本:
cj = cookielib.MozillaCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=0)
br.addheaders = [('User-agent', 'Mozilla')]
br.open('http://website.com')
for f in br.forms():
print f
br.select_form(nr=0)
br.form['user[username]'] = 'john'
br.form['user[password]'] = 'secret'
response = br.submit()
cj.save(cookie_filename, ignore_discard=True, ignore_expires=True)