How to save requests (python) cookies to a file?
There is no immediate way to do so, but it's not hard to do.
You can get a CookieJar
object from the session with session.cookies
, and use pickle
to store it to a file.
A full example:
import requests, picklesession = requests.session()# Make some callswith open('somefile', 'wb') as f: pickle.dump(session.cookies, f)
Loading is then:
session = requests.session() # or an existing sessionwith open('somefile', 'rb') as f: session.cookies.update(pickle.load(f))
The requests
library uses the requests.cookies.RequestsCookieJar()
subclass, which explicitly supports pickling and a dict-like API. The RequestsCookieJar.update()
method can be used to update an existing session cookie jar with the cookies loaded from the pickle file.
After a call such as r = requests.get()
, r.cookies
will return a RequestsCookieJar
which you can directly pickle
, i.e.
import pickledef save_cookies(requests_cookiejar, filename): with open(filename, 'wb') as f: pickle.dump(requests_cookiejar, f)def load_cookies(filename): with open(filename, 'rb') as f: return pickle.load(f)#save cookiesr = requests.get(url)save_cookies(r.cookies, filename)#load cookies and do a requestrequests.get(url, cookies=load_cookies(filename))
If you want to save your cookies in human-readable format, you have to do some work to extract the RequestsCookieJar
to a LWPCookieJar
.
import cookielibdef save_cookies_lwp(cookiejar, filename): lwp_cookiejar = cookielib.LWPCookieJar() for c in cookiejar: args = dict(vars(c).items()) args['rest'] = args['_rest'] del args['_rest'] c = cookielib.Cookie(**args) lwp_cookiejar.set_cookie(c) lwp_cookiejar.save(filename, ignore_discard=True)def load_cookies_from_lwp(filename): lwp_cookiejar = cookielib.LWPCookieJar() lwp_cookiejar.load(filename, ignore_discard=True) return lwp_cookiejar#save human-readabler = requests.get(url)save_cookies_lwp(r.cookies, filename)#you can pass a LWPCookieJar directly to requestsrequests.get(url, cookies=load_cookies_from_lwp(filename))
Expanding on @miracle2k's answer, requests Session
s are documented to work with any cookielib CookieJar
. The LWPCookieJar
(and MozillaCookieJar
) can save and load their cookies to and from a file. Here is a complete code snippet which will save and load cookies for a requests session. The ignore_discard
parameter is used to work with httpbin for the test, but you may not want to include it your in real code.
import osfrom cookielib import LWPCookieJarimport requestss = requests.Session()s.cookies = LWPCookieJar('cookiejar')if not os.path.exists('cookiejar'): # Create a new cookies file and set our Session's cookies print('setting cookies') s.cookies.save() r = s.get('http://httpbin.org/cookies/set?k1=v1&k2=v2')else: # Load saved cookies from the file and use them in a request print('loading saved cookies') s.cookies.load(ignore_discard=True) r = s.get('http://httpbin.org/cookies')print(r.text)# Save the session's cookies back to the files.cookies.save(ignore_discard=True)