Web Scraping24 min read
Sessions and Cookies
Understand cookies and sessions, and learn how to maintain state across multiple requests like a real browser.
David Miller
January 9, 2026
0.5k10
Websites remember you using cookies.
Cookies store:
- login info
- preferences
- session ids
If you don’t keep them, the site may treat every request as new.
Why sessions matter
Without sessions:
- you may get logged out
- pagination may break
- site may block you
Using requests.Session
import requests
session = requests.Session()
session.headers.update({
"User-Agent": "Mozilla/5.0"
})
res1 = session.get("https://example.com")
res2 = session.get("https://example.com/profile")
Both share cookies automatically.
Inspect cookies
print(session.cookies)
When to use sessions
- after login
- multi-step forms
- sites with tracking
Graph: session flow
flowchart TD
A[First Request] --> B[Receive Cookies]
B --> C[Session Stores Cookies]
C --> D[Next Request]
D --> E[Same Identity]
Remember
- Session acts like a browser
- Cookies maintain identity
- Always use Session for multi-step scraping
#Python#Beginner#Sessions