Sunday, December 14, 2025
HomeLanguagesRetrieving Cookies in Python

Retrieving Cookies in Python

Retrieving cookies in Python can be done by the use of the Requests library. Requests library is one of the integral part of Python for making HTTP requests to a specified URL. The below codes show different approaches to do show:

1. By requesting a session:




# import the requests library
import requests
  
# initialize a session
session = requests.Session()
  
# send a get request to the server
response = session.get('http://google.com')
  
# print the response dictionary
print(session.cookies.get_dict())



Output:

{‘1P_JAR’: ‘2020-04-30-07’, ‘NID’: ‘203=GIlzlNytcSGjMtV-ML49xgKQax4NACMgFZi56sbQ3tSd9uDqL7EWZ6KC_gUqPsKkk-XsDIlca8ElKqhjsHGgWrPRwbbPBFXxcGL_G5Jd0gzdQYhCo-QALsZm4zItqIeImlBBTp_TDOgRQIW0d2hSNerxmQkljluhIA3QGLgNLnM’}

2.By requesting cookies list from the server:




import requests
  
r = requests.get('http://google.com')
  
for c in r.cookies:
    print(c.name +"==>>", c.value)


Output:

1P_JAR==>> 2020-04-30-07

NID==>> 203=YvFCKkIeHS4SvpTVv8jw6MIGEU54IlN8V_1aQZrXmOU7Zdj74qZdW69E3A38KSP-GrE5xLmR40ozrHTFxkXm4-iaTm4DbhU4cwmvOWHEs1OZELI8H8KQLmBLCxccxCuHT07QQ2mqc-ppBYXhcHtOC7idVc9RWD2kmNPDMR-YMl4

RELATED ARTICLES

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6953 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS