gpt4 book ai didi

Python 请求修改现有 cookie 值的正确方法是什么?

转载 作者:可可西里 更新时间:2023-11-01 17:07:22 25 4
gpt4 key购买 nike

我正在访问一个网页,该网页创建了一个带有值的 cookie,然后修改该值并从同一网站访问另一个页面。在 python 中使用 librequests 我得到了以下 cookie:s 是使用 s = requests.Session()

打开的 session
In [63]: s.cookies
Out[63]: <RequestsCookieJar[Cookie(version=0, name='my_cookie', value='normal_value', port=None, port_specified=False, domain='my_domain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=False, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>

我尝试了一些关于请求的事情,首先是:

[74]: s.cookies.set('my_cookie','new_value')
Out[74]: Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='/mydomain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)

但作为返回我得到了

In [75]: s.cookies
Out[75]: <RequestsCookieJar[Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='/mydomain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False),
Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='mydomain.lol', domain_specified=False, domain_initial_dot=False, path='/my_path', path_specified=False, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>

如您所见,我的新值并没有替换旧值,而是创建了一个新的 cookie,使用以下方法获得了相同的结果:

s.cookies['my_cookie'] = 'new_value'

然后我在设置 cookie 时尝试指定尽可能多的东西,它起作用了:

In [67]: s.cookies.set('my_cookie','new_value',domain='mydomain.lol',path='/my_path')
Out[67]: Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='mydomain.lol', domain_specified=True, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)

In [68]: s.cookies
Out[68]: <RequestsCookieJar[Cookie(version=0, name='my_cookie', value='new_value', port=None, port_specified=False, domain='mydomain.lol', domain_specified=True, domain_initial_dot=False, path='/my_path', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>

因此我的问题是,有没有更方便的方式来设置 cookie 而无需指定这么多东西?例如,通过获取数组的第一个 cookie?

最佳答案

可以先将值设为None:

  s.cookies.set('cookie', None)
s.cookies.set('cookie', "new_value")

一个例子:

In [5]: import requests


In [6]: with requests.Session() as s:
...: s.get('http://httpbin.org/cookies/set?c1=foo&c2=bar')
...: r = s.get('http://httpbin.org/cookies')
...: print(r.text)
...: s.cookies.set('c1', None)
...: s.cookies.set('c1', "foobar")
...: print(s.cookies)
...: r = s.get('http://httpbin.org/cookies')
...: print(r.text)
...:
{
"cookies": {
"c1": "foo",
"c2": "bar"
}
}

<<class 'requests.cookies.RequestsCookieJar'>[<Cookie c1=foobar for />, <Cookie c2=bar for httpbin.org/>]>
{
"cookies": {
"c1": "foobar",
"c2": "bar"
}
}

关于Python 请求修改现有 cookie 值的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com