About Using Cookies with the Requests Library

When you want to use and preserve cookies with the requests library, use the methods of requests.Session() object instead of requests:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import requests
session = requests.Session()
session.cookies.set(
    "cookie_consent", 
    "functionality|performance|marketing",
    domain="www.djangotricks.com",
)
r = session.get(
   "https://www.djangotricks.com/tricks/3PQVqes4mJRa/"
)
assert("console.log('Conditional performance code loaded');" in r.text)

Tips and Tricks Programming Development Python 3 Cookies