🚀 We are hiring! See open positions

How to save and load cookies in Python requests?

by scrapecrow Dec 08, 2022

HTTP cookies in web scraping play a vital role across several factors, including blocking and localization. Hence, pausing and resuming request sessions is often useful. For this, we'll explain how to save and load Python requests cookies.

Save Cookies

Let's start by saving cookies in Python requests. For this, we'll use the requests.cookiejar utility package:

from pathlib import Path
from requests.utils import dict_from_cookiejar
import json
import requests

# create a session to presists cookies
session = requests.session()

# send cookies through a response object
session.get("https://httpbin.dev/cookies/set?key1=value1&key2=value2")

# turn cookiejar into dict
cookies = dict_from_cookiejar(session.cookies)  

# save them to file as JSON
Path("cookies.json").write_text(json.dumps(cookies))

Above, we create a new session object and request a URL to accept few cookie values. Then, we save the retrieved Python request cookies to an object using the dict_from_cookiejar method. Finally, the cookie data is saved to a JSON file:

{"key1": "value1", "key2": "value2"}

Load Cookies

Now that we saved the cookie object using Python cookiejar, let's load it:

from pathlib import Path
from requests.utils import cookiejar_from_dict
import json
import requests

# create a session
session = requests.session()

# load the JSON file
cookies = json.loads(Path("cookies.json").read_text())

# turn the object into a a cookie jar
cookies = cookiejar_from_dict(cookies)

# load cookiejar to current session
session.cookies.update(cookies)

# validate the cookies
print(session.get("https://httpbin.dev/cookies").text)
{"key1": "value1", "key2": "value2"}

Here, we started by loading the JSON file and importing the cookie values using the cookiejar_from_dict. Next, we let the requests set cookie values by loading them into the session, which will automatically set cookie header values.

For further details on utilizing cookies for requests in Python, refer to our dedicated guide.

How to Handle Cookies in Web Scraping

Introduction to cookies in web scraping. What are they and how to take advantage of cookie process to authenticate or set website preferences.

How to Handle Cookies  in Web Scraping

Related Articles

Guide to Python requests POST method

Discover how to use Python's requests library for POST requests, including JSON, form data, and file uploads, along with response handling tips.

PYTHON
REQUESTS
HTTP
Guide to Python requests POST method

Guide to Python Requests Headers

Our guide to request headers for Python requests library. How to configure and what do they mean.

PYTHON
REQUESTS
HTTP
Guide to Python Requests Headers

How to Scrape AutoScout24

Learn how to scrape AutoScout24 for car listings, prices, specifications, and detailed vehicle information using Python. Complete guide with code examples and anti-blocking techniques.

PYTHON
SCRAPEGUIDE
BEAUTIFULSOUP
REQUESTS
How to Scrape AutoScout24

How to Scrape Ticketmaster Event Data

Learn how to scrape Ticketmaster for event data including concerts, venues, dates, and ticket information using Python. Complete guide with code examples and anti-blocking techniques.

PYTHON
SCRAPEGUIDE
BEAUTIFULSOUP
REQUESTS
How to Scrape Ticketmaster Event Data

How to Scrape Mouser.com

Learn how to scrape Mouser.com electronic component data including prices, specifications, and inventory using Python. Complete guide with code examples and anti-blocking techniques.

PYTHON
SCRAPEGUIDE
BEAUTIFULSOUP
REQUESTS
How to Scrape Mouser.com

How to Scrape Zoro.com

Learn how to scrape Zoro.com product data including prices, specifications, and inventory using Python. Complete guide with code examples and anti-blocking techniques.

PYTHON
SCRAPEGUIDE
BEAUTIFULSOUP
REQUESTS
How to Scrape Zoro.com