89
x = requests.post(url, data=data)
print x.cookies

I used the requests library to get some cookies from a website, but I can only get the cookies from the Response, how to get the cookies from the Request? Thanks!

kryger
  • 12,416
  • 8
  • 43
  • 65
Danfi
  • 1,042
  • 1
  • 10
  • 14

2 Answers2

159

Alternatively, you can use requests.Session and observe cookies before and after a request:

>>> import requests
>>> session = requests.Session()
>>> print(session.cookies.get_dict())
{}
>>> response = session.get('http://google.com')
>>> print(session.cookies.get_dict())
{'PREF': 'ID=5514c728c9215a9a:FF=0:TM=1406958091:LM=1406958091:S=KfAG0U9jYhrB0XNf', 'NID': '67=TVMYiq2wLMNvJi5SiaONeIQVNqxSc2RAwVrCnuYgTQYAHIZAGESHHPL0xsyM9EMpluLDQgaj3db_V37NjvshV-eoQdA8u43M8UwHMqZdL-S2gjho8j0-Fe1XuH5wYr9v'}
SiHa
  • 6,756
  • 12
  • 30
  • 41
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
  • 5
    This will not return the `path` and `domain`, any suggestions? – Or Duan Oct 18 '16 at 09:12
  • 3
    @OrDuan as noted [here](https://stackoverflow.com/a/36387083/426790) you can do `r = requests.get('...')` and then iterate over the Cookie objects using `r.cookies._cookies` which is a dictionary whose keys are `[][]` – Greg Sadetsky Dec 08 '18 at 20:20
  • 1
    @GregSadetsky oh wow that was years ago, I'm posting the solution I used years ago as an answer :) – Or Duan Dec 08 '18 at 20:30
  • @GregSadetsky I am getting a weird behavior, since mine `r.cookies._cookies` is returning an empty dictionary, while when I access with Firefox or Chrome, I can find them easily with developer tools. Any hint on that? – Ricardo Barros Lourenço Jan 27 '20 at 13:13
  • 1
    @RicardoBarrosLourenço just tried it with requests 2.20.1 and both iterating over `r.cookies` (and getting the `domain` attribute) or accessing `r.cookies._cookies` worked. Could you try it with `http://google.com` ? Could the URL you're accessing be returning something different because of the different user-agent? If you set requests' user-agent to Firefox's, does it work? – Greg Sadetsky Jan 28 '20 at 16:27
  • @GregSadetsky when using `google.com` it works. I assume that some routine in Javascript is responsible for rendering such cookies. – Ricardo Barros Lourenço Jan 28 '20 at 18:36
  • @RicardoBarrosLourenço great point, cookies set by the JavaScript code could be (/ are most probably) the culprit..! – Greg Sadetsky Jan 29 '20 at 19:27
  • It's 2021, you can do `r = requests.get(https://...)` then `r.cookies.get_dict()` – Aditya Rajgor Nov 07 '21 at 07:25
17

If you need the path and thedomain for each cookie, which get_dict() is not exposes, you can parse the cookies manually, for instance:

[
    {'name': c.name, 'value': c.value, 'domain': c.domain, 'path': c.path}
    for c in session.cookies
]
Or Duan
  • 11,648
  • 4
  • 56
  • 64