Skip to content
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

Closed
mghadam opened this issue Apr 14, 2016 · 4 comments
Closed

cookiejar file #37

mghadam opened this issue Apr 14, 2016 · 4 comments

Comments

@mghadam
Copy link

mghadam commented Apr 14, 2016

Is it possible to set a cookiejar file so that the sessions could be saved and used several times?

@andpozo
Copy link

andpozo commented May 4, 2016

@mghadam:

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:

A CookieJar containing all currently outstanding cookies set on this session. By default it is a RequestsCookieJar, but may be any other cookielib.CookieJar compatible object.

and from http://docs.python-requests.org/en/master/api/#requests.cookies.RequestsCookieJar

Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.

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)

@hickford
Copy link
Collaborator

hickford commented Oct 7, 2016

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

import pickle
pickle.dump(browser, open("browser.obj", 'wb'))

pickle.load(open("browser.obj", 'rb'))

Perhaps this deserves to go in the documentation

@hickford hickford closed this as completed Oct 7, 2016
@amitpdev
Copy link

amitpdev commented Apr 14, 2018

Dumping StatefulBrowser with pickle may produce error "RecursionError: maximum recursion depth exceeded".

I found this more appropriate:

# Save cookieJar from active session
pickle.dump(browser.get_cookiejar(), open("cookiejar.dump", 'wb'))
# Load cookiejar into fresh browser
cookiejar = pickle.load(open("cookiejar.dump", 'rb'))
browser.set_cookiejar(cookiejar)

@hemberger
Copy link
Contributor

Alternatively, you can use a FileCookieJar, like LWPCookieJar:

# 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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants