简体   繁体   English

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

[英]Trying to load cookie into requests session from dictionary

I'm working with the python requests library.我正在使用 python 请求库。 I am trying to load a requests session with a cookie from a dictionary:我正在尝试使用字典中的 cookie 加载请求 session:

cookie = {'name':'my_cookie','value': 'kdfhgfkj' ,'domain':'.ZZZ.org', 'expires':'Fri, 01-Jan-2020 00:00:00 GMT'}

I've tried:我试过了:

s.cookies.set_cookie(cookie)

but this gives:但这给出了:

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'

What am I doing wrong?我究竟做错了什么?

cookies has a dictionary-like interface, you can use update() : cookies具有类似于字典的界面,您可以使用update()

s.cookies.update(cookie)

Or, just add cookies to the next request: 或者,只需将cookies添加到下一个请求:

session.get(url, cookies=cookie)

It would "merge" the request cookies with session cookies and the newly added cookies would be retained for subsequent requests, see also: 它将“合并”请求cookie和会话cookie,新添加的cookie将保留用于后续请求,另请参见:

this doesn't seem to work for me for multiple cookies, they just get overwritten so the last one to get added is the only cookie in the jar.对于多个 cookies,这似乎对我不起作用,它们只是被覆盖,所以最后添加的是 jar 中唯一的 cookie。

For example, my multiple cookies (from Flaresolverr (which I believe is the same output as selenium)) outputs multiple dictionaries in a list, eg:例如,我的多个 cookies(来自 Flaresolverr(我相信它与 selenium 相同的 output))在一个列表中输出多个字典,例如:

cookies = [
      {
        "name": "ASP.NET_SessionId",
        "value": "SOMEVALUE",
        "domain": "my.domain.co.uk",
        "path": "/",
        "expires": -1,
        "size": 41,
        "httpOnly": true,
        "secure": false,
        "session": true
      },
      {
        "name": "__cfduid",
        "value": "SOMEVALUE",
        "domain": ".mydomain.co.uk",
        "path": "/",
        "expires": 1622898293.967355,
        "size": 51,
        "httpOnly": true,
        "secure": true,
        "session": false,
        "sameSite": "Lax"
      }
   ]

So if I then try to add them all in a cookie jar, you can see " __cfduid " is the only cookie in the jar:因此,如果我随后尝试将它们全部添加到 cookie jar 中,您会看到“ __cfduid ”是 jar 中唯一的 cookie:

r = requests.session()
for c in cookies:
      r.cookies.update(c)

print(r.cookies)
<RequestsCookieJar[<Cookie domain=.mydomain.co.uk for />, <Cookie expires=1622898293.967355 for />, <Cookie httpOnly=True for />, <Cookie name=__cfduid for />, <Cookie path=/ for />, <Cookie sameSite=Lax for />, <Cookie secure=True for />, <Cookie session=False for />, <Cookie size=51 for />, <Cookie value=SOMEVALUE for />]>

I've also tried jar = requests.cookies.RequestsCookieJar() and jar.set(xxxx) but it doesn't like non-standard cookie attributes (eg httpOnly)我也尝试过jar = requests.cookies.RequestsCookieJar()和 jar.set(xxxx) 但它不喜欢非标准的 cookie 属性(例如 httpOnly)

Any help would be very much appreciated, thank you!任何帮助将不胜感激,谢谢!

session.cookies.set_cookie 's parameter should be Cookie object , NOT dict (of cookie) session.cookies.set_cookie的参数应该是Cookie object ,不是dict (of cookie)

if you want add new cookie into session.cookies from dict, you can use:如果你想从 dict 添加新的 cookie 到session.cookies ,你可以使用:

or要么

more detail pls refer to my another post's answer更详细的请参考我另一个帖子的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM