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

标签 python http cookies python-requests

我正在访问一个网页,该网页创建了一个带有值的 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/

相关文章:

php - 如何用php管理cookies

java - 将 cookie 从域传递到子域

Javascript:添加 CSS 类并保存到 cookie

python - Dronekit-python 在 docker 中运行,连接到主机上的 MAVProxy

python - Django Restful框架身份验证HTTP Post错误

python - 如何在python中为热图的特定元素添加颜色边框或类似的突出显示?

api - HTTP 状态代码 >= 300 返回 JSON

python - pandas dataframe 聚合固定数量的行

javascript - 有人可以解释 ENOBUFS 错误吗?

http - Web Dev Helper,如 IE8 中的 HTTP 请求跟踪