user2578535
user2578535

Reputation: 147

Handling cookiejar from requests library python

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

Answers (2)

Kkelk
Kkelk

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

chishaku
chishaku

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']

More from the docs

Upvotes: 2

Related Questions