pythonpython-2.7cookiespython-requestscookiejar

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.


Solution

  • 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.