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

FlareSolverr Guide: Bypass Cloudflare While Scraping

In this article, we'll explore the FlareSolverr tool and how to use it to get around Cloudflare while scraping. We'll start by explaining what FlareSolverr is, how it works, how to install and use it. Let's get started!

PYTHON
TOOLS
BLOCKING
HTTP
FlareSolverr Guide: Bypass Cloudflare While Scraping

How to Effectively Use User Agents for Web Scraping

In this article, we’ll take a look at the User-Agent header, what it is and how to use it in web scraping. We'll also generate and rotate user agents to avoid web scraping blocking.

HTTP
PYTHON
How to Effectively Use User Agents for Web Scraping

How to Scrape in Another Language, Currency or Location

Localization allows for adapting websites content by changing language and currency. So, how do we scrape it? We'll take a look at the most common methods for changing language, currency and other locality details in web scraping.

PYTHON
HEADLESS-BROWSER
HTTP
How to Scrape in Another Language, Currency or Location

Web Scraping Graphql with Python

Introduction to web scraping graphql powered websites. How to create graphql queries in python and what are some common challenges.

HTTP
GRAPHQL
PYTHON
Web Scraping Graphql with Python