-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cookiejar file #37
Comments
MechanicalSoup is a wrapper for python requests and uses a Session class for browser session. from http://docs.python-requests.org/en/master/api/#requests.Session.cookies:
and from http://docs.python-requests.org/en/master/api/#requests.cookies.RequestsCookieJar
so... I assume that the MechanicalSoup uses a CookieJar if not you can initialize your Browser with a custom session: import requests
import mechanicalsoup
from http import cookiejar
c = cookiejar.CookieJar() #you can chooise another cookie store like a LWPCookieJar
s = requests.Session()
s.cookies = c
browser = mechanicalsoup.Browser(session=s) |
Right. mechanicalsoup.Browser wraps requests.Session, so it automatically stores cookies for the lifetime of the Browser object. If you wish to save cookies between executions of your app, you can pickle the Browser object (or the session, or the cookie jar). See https://docs.python.org/3/library/pickle.html
Perhaps this deserves to go in the documentation |
Dumping StatefulBrowser with pickle may produce error "RecursionError: maximum recursion depth exceeded". I found this more appropriate:
|
Alternatively, you can use a # Load cookies from file or create new cookie file
cookiejar = cookielib.LWPCookieJar(cookie_file)
if os.path.exists(cookie_file):
cookiejar.load()
browser.set_cookiejar(cookiejar)
# Do something that sets cookies here
# Save cookies to file
cookiejar.save() |
Is it possible to set a cookiejar file so that the sessions could be saved and used several times?
The text was updated successfully, but these errors were encountered: