I'm using mechanize and python to log into a site. I've created two functions. The first one logs in and the second one searches the site. How exactly do I store the cookies from the login so when I come to searching I have a cookie.
Current code.
import mechanize
import cookielib
def login(username, password):
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
cj.save('cookies.txt', ignore_discard=False, ignore_expires=False)
# Rest of login
def search(searchterm):
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
cj.load('cookies.txt', ignore_discard=False, ignore_expires=False)
# Rest of search
I read through the cookielib info page but there aren't many examples there and I haven't been able to get it working. Any help would be appreciated. Thanks