I have looked through similar questions in stackoverflow yet seemingly no answers measures up. Now I have a cookies.txt in hands which is exported by a chrome extension named “cookies.txt”. I can execute a command “wget –load-cookies cookies.txt http://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.