How to save requests (python) cookies to a file?

agrynchuk picture agrynchuk · Oct 23, 2012 · Viewed 72.7k times · Source

How to use the library requests (in python) after a request

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
bot = requests.session()
bot.get('http://google.com')

to keep all the cookies in a file and then restore the cookies from a file.

Answer

madjar picture madjar · Oct 23, 2012

There is no immediate way to do so, but it's not hard to do.

You can get a CookieJar object from the session as session.cookies, you can use pickle to store it to a file.

A full example:

import requests, pickle
session = requests.session()
# Make some calls
with open('somefile', 'wb') as f:
    pickle.dump(session.cookies, f)

Loading is then:

session = requests.session()  # or an existing session

with open('somefile', 'rb') as f:
    session.cookies.update(pickle.load(f))

The requests library has uses the requests.cookies.RequestsCookieJar() subclass, which explicitly supports pickling and a dict-like API, and you can use the RequestsCookieJar.update() method to update an existing session cookie jar with those loaded from a pickle file.