尝试将cookie加载到字典的请求会话中

use*_*629 2 python cookies session python-requests

我正在使用python请求库。我正在尝试使用字典中的cookie加载请求会话:

cookie = {'name':'my_cookie','value': 'kdfhgfkj' ,'domain':'.ZZZ.org', 'expires':'Fri, 01-Jan-2020 00:00:00 GMT'}
Run Code Online (Sandbox Code Playgroud)

我试过了:

s.cookies.set_cookie(cookie)
Run Code Online (Sandbox Code Playgroud)

但这给出了:

File "....lib\site-packages\requests\cookies.py", line 298, in set_cookie
    if hasattr(cookie.value, 'startswith') and cookie.value.startswith('"') and cookie.value.endswith('"'):
AttributeError: 'dict' object has no attribute 'value'
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

ale*_*cxe 6

cookies具有类似于字典的界面,可以使用update()

s.cookies.update(cookie)
Run Code Online (Sandbox Code Playgroud)

或者,只需添加cookies到下一个请求:

session.get(url, cookies=cookie)
Run Code Online (Sandbox Code Playgroud)

它将“合并”请求cookie和会话cookie,新添加的cookie将保留用于后续请求,另请参见: