I have code similar to this:
br = mechanize.Browser()
br.open("https://mysite.com/")
br.select_form(nr=0)
#do stuff here
response = br.submit()
html = response.read()
#now that i have the login cookie i can do this...
br.open("https://mysite.com/")
html = response.read()
However, my script is responding like it's not logged in for the second request. I checked the first request and yes, it logs in successfully. My question is: do cookies in Mechanize browsers need to be managed or do I need to setup a CookieJar or something, or does it keep track of all of them for you?
The first example here talks about cookies being carried between requests, but they don't talk about browsers.
Yes you will have to store the
cookie
betweenopen
requests inmechanize
. Something similar to the below should work as you can add thecookiejar
to thebr
object and as long as that object exists it maintains that cookie.The Docs cover it in more detail.
I use
perl
mechanize
alot, but notpython
so I may have missed somethingpython
specific for this to work, so if I did I apologize, but I did not want to answer with a simpleyes
.