4

How can I load a CookieJar to a new requests.Session object?

cj = cookielib.MozillaCookieJar("mycookies.txt")
s = requests.Session()

This is what I create, now the session will store cookies, but I want it to have my cookies from the file
(The session should load the cookieJar). How can this be achieved?
I searched the documentation but I can only find code examples and they are never loading a cookieJar, just saving cookies during one session.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Ranama
  • 131
  • 1
  • 3
  • 10

3 Answers3

6

Python 3.x code, fully working and well-implemented example. The code is self-explanatory.

This code properly handles "session cookies", preserving them between runs. By default, those are not saved to disk, which means that most websites would require you to constantly login between runs. But with the technique below, all session cookies are kept too!

This is the code you are looking for.

import os
import pathlib
import requests
from http.cookiejar import MozillaCookieJar


cookiesFile = str(pathlib.Path(__file__).parent.absolute() / "cookies.txt")  # Places "cookies.txt" next to the script file.
cj = MozillaCookieJar(cookiesFile)
if os.path.exists(cookiesFile):  # Only attempt to load if the cookie file exists.
    cj.load(ignore_discard=True, ignore_expires=True)  # Loads session cookies too (expirydate=0).

s = requests.Session()
s.headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
    "Accept-Language": "en-US,en"
}
s.cookies = cj  # Tell Requests session to use the cookiejar.

# DO STUFF HERE WHICH REQUIRES THE PERSISTENT COOKIES...
s.get("https://www.somewebsite.com/")

cj.save(ignore_discard=True, ignore_expires=True)  # Saves session cookies too (expirydate=0).
Mitch McMabers
  • 3,634
  • 28
  • 27
5

In Python 3.x

import requests
import http.cookiejar

s = requests.Session()
s.cookies = http.cookiejar.MozillaCookieJar("anything.txt")

for example, i will acces google site and save the cookiejar to file "anything.txt"

s.get("https://google.com")
s.cookies.save()

And in the future, i access google again with my cookiejar.

s.cookies.load()
s.get("https://google.com")
fireattack
  • 133
  • 1
  • 8
  • 1
    `LWPCookieJar` is weird (obscure Perl-style cookies). Most people should use `MozillaCookieJar` instead, which is the industry-standard cookies.txt format. – Mitch McMabers May 31 '20 at 05:32
1

There's an optional cookies= that can be provided for a requests.Session (as well as request) objects:

cookies = None

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.

see: https://2.python-requests.org/en/latest/api/#requests.Session.cookies

So it becomes:

s = requests.Session(cookies=cj)

Update: I was confusing the the requests.get, request.post etc..., as correctly pointed out by mata in comments - cookies is an attribute of the session object, not a init parameter, so this won't work. s.cookies = cj after constructing the session will:

Therefore, use:

s = requests.Session()
s.cookies = cj
Nemo
  • 2,441
  • 2
  • 29
  • 63
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • 1
    `cookies` is an _attribute_ of the session object, not a init parameter, so this won't work. `s.cookies = cj` after constructing the session will. – mata Mar 22 '15 at 22:19
  • @mata you're absolutely right - thanks for that - edited – Jon Clements Mar 22 '15 at 22:25
  • This no longer works: `TypeError: __init__() got an unexpected keyword argument 'cookies'` – BugHunterUK Aug 17 '16 at 15:31
  • I believe the breaking change is documented here: https://github.com/kennethreitz/requests/blob/ff0c325014f817095de35013d385e137b111d6e8/docs/api.rst#api-changes – ambiso Apr 15 '19 at 20:31