I have looked through similar questions in stackoverflow yet seemingly no answers measures up. Now I have a cookies.txt in hands whic开发者_C百科h is exported by a chrome extension named "cookies.txt". I can execute a command "wget --load-cookies cookies.txt www.example.com" to download the webpage with the account authenticated.
However, I met a problem when I tried to use this file in my python script as follows,
import mechanize
cookie = 'cookies.txt'
cookiejar = mechanize.FileCookieJar(cookies.txt)
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_cookiejar(cookiejar)
url = 'www.example.com'
response = br.open(url)
s = response.read()
f = open('test.html','w')
f.write(s)
f.close()
I only got a webpage without my account logged in after executing this script. And If I change the first several lines of code into this
import mechanize
cookie = 'cookies.txt'
cookiejar = mechanize.MozillaCookieJar()
cookiejar.load(cookie)
I got an error message "mechanize._clientcookie.LoadError: cookies.txt does not look like a Netscape format cookies file" executing the script.
I have no idea how I can get the authentication done with this cookies.txt given that this file works in wget command.
I was getting the same error until I added this to the top of my cookie file, and now works.
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
According to the mechanize source code, the load method of class MozillaCookieJar will search for the regexp "#( Netscape)? HTTP Cookie File" first, so you can insert this line into the top of the exported cookie.txt:
# Netscape HTTP Cookie File
Use the cookielib module to handle the cookies.
http://docs.python.org/library/cookielib.html
Specifically, you need to read up on how to use FileCookieJar.load()
That should get you on track.
精彩评论