Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After a call such as <code>r = requests.get()</code>, <code>r.cookies</code> will return a <code>RequestsCookieJar</code> which you can directly <a href="http://docs.python.org/2/library/pickle.html"><code>pickle</code></a>, i.e.</p> <pre><code>import pickle def 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 cookies r = requests.get(url) save_cookies(r.cookies, filename) #load cookies and do a request requests.get(url, cookies=load_cookies(filename)) </code></pre> <p>If you want to save your cookies in human-readable format, you have to do some work to extract the <code>RequestsCookieJar</code> to a <a href="http://docs.python.org/2/library/cookielib.html#cookielib.LWPCookieJar"><code>LWPCookieJar</code></a>.</p> <pre><code>import cookielib def 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-readable r = requests.get(url) save_cookies_lwp(r.cookies, filename) #you can pass a LWPCookieJar directly to requests requests.get(url, cookies=load_cookies_from_lwp(filename)) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload