Reputation: 147
I'm having a problem with retrieving cookies from a cookiejar.
I'm trying to use the requests library, and I want to retrieve a specific cookie, but I don't know how to.
Maybe one of you have more experience than I do or maybe knows more about Python2.7 and can help me out :)
Example:
import requests
r = requests.post('url')
print r.cookies
I'd like to handle this like a list or an array, so I can just get the value of a specific cookie.
Thanks in advance.
Upvotes: 1
Views: 3504
Reputation: 136
If you want the value of a specific cookie (that you know by name), then as per the Python Requests documentation it is possible to access this just like a dictionary:
import requests
r = requests.post('url')
r.cookies['cookie_name']
If, as you say, you'd like them as a list then you can use r.cookies.keys()
to return a list of all the keys (cookie names) which you can then access as per the above, or just r.cookies.values()
to get a list of all the values.
Upvotes: 3
Reputation: 4643
First make sure that 'url' is a complete url e.g. 'http://example.com'
The cookies are stored as a dictionary. To access a specific cookie:
r.cookies['example_cookie_name']
Upvotes: 2