How to change cookies in requests

I could not find any cookie modification documents on the official website, i.e. no api doc for requests.cookies.RequestsCookieJar .

For example,

 session = requests.Session() a = session.head('http://www.google.co.uk') session.cookies 

<[Cookies (version = 0, name = 'NID', value = '67 = CXdvwjj9sjd-13Y0VyRQyUs8PxXaxyMhiGrrozXP7RWSjf-5alV4D17ORcfnZNYLAmlHXSVlHuS5LcuE4-v6vnzRQS-Gt72hgbGye0apoBoW5KJeVXA2o2E0gE-8jIeY ", port = None, port_specified = False, domain = '. Google.co.uk' , domain_specified = True, domain_initial_dot = True, path = '/', path_specified = True, secure = False, expires = 1424443599, discard = False, comment = None, comment_url = None, rest = {'HttpOnly': None}, rfc2109 = False), Cookie (version = 0, name = 'PREF', Value = 'J = 41c5d5cac7d22262: = 0: = 1408632399: LM = 1408632399: S = wTfY_LkkZnSsBxoL', port = None, port_specified = False, domain = '. google.co.uk', domain_specified = True, domain_initial_dot = True, path = '/', path_specified = True, secure = False, expires = 1471704399, discard = False, comment = None, comment_url = None, rest = { }, rfc2109 = False)]>

Now I want to change the value of 'NID'

If I do session.cookies['NID'] = 'abc' , it would have duplicate keys like:

<[cookie (version = 0, name = 'NID', value = 'abc', port = None, port_specified = False, domain = '', domain_specified = False, domain_initial_dot = False, path = '/', path_specified = True , secure = False, expires = None, discard = True, comment = None, comment_url = None, rest = {'HttpOnly': None}, rfc2109 = False), Cookie (version = 0, name = 'NID', value = '67 = CXdvwjj9sjd-13Y0VyRQyUs8PxXaxyMhiGrrozXP7RWSjf-5alV4D17ORcfnZNYLAmlHXSVlHuS5LcuE4-v6vnzRQS-Gt72hgbGye0apoBoW5KJeVXA2o2E0gE-8jIeY ", port = None, port_specified = False, domain = '. google.co.uk', domain_specified = True, domain_initial_dot = True, path = '/', path_specified = True, secure = False, expires = 1424443599, discard = False, comment = None, comment_url = None, rest = {'HttpOnly': None}, rfc2109 = False), Cookie (version = 0, name = 'PREF', Value = 'J = 41c5d5cac7d22262: = 0: = 1408632399: LM = 1408632399: S = wTfY_LkkZnSsBxoL', port = None, port_specified = False, domain = '. G oogle.co.uk ', domain_specified = True, domain_initial_dot = True, path =' / ', path_specified = True, secure = False, expires = 1471704399, discard = False, comment = None, comment_url = None, rest = {}, rfc2109 = false)]>

My current approach is to make session.cookies['NID'] = None first, this removes the key / value, and then session.cookies['NID'] = 'abc' This sometimes works, but completely ignores cookie attributes.

What is the right way to do this?

+7
python python-requests
source share
1 answer

As you can see, your cookie does not have a domain specified for it, so this is actually another cookie.

Use of domain and path

  session.cookies.set('NID', 'abc', domain='.google.co.uk', path='/') 

will set a new cookie instead of the previously defined one.

RequestCookieJar is a wrapper for cookielib.CookieJar , but if you want to change the cookie attributes in place (so that you reference the actual cookielib.Cookie objects), I found a better way than using an iterator.

If you look at the sources of requests.cookies.RequestsCookieJar , then there are no other methods that allow you to access the elements yourself, and not to the name / value fields.

+2
source share

All Articles