gpt4 book ai didi

Python 请求包含 cookie 错误

转载 作者:行者123 更新时间:2023-12-02 04:02:11 34 4
gpt4 key购买 nike

我正在尝试使用 Requests 模块在 python 上发出 get 请求,同时合并现有的 Cookie,这是我的代码的样子:

import requests

url="https://stackoverflow.com/"
headers = {"User-Agent", "Mozilla/5.0"}
cookie = {
"domain": ".stackoverflow.com",
"expirationDate": "1458316186",
"hostOnly": "false",
"httpOnly": "false",
"name": "__qca",
"path": "/",
"secure": "false",
"session": "false",
"storeId": "0",
"value": "P0-SOMEVALUE-SOMEVALUE",
"id": 1
}

print requests.get(url, cookies=cookie).text


Traceback (most recent call last):
File "test.py", line 19, in <module>
print requests.get(url, cookies=cookie).text
File "C:\Python27\lib\site-packages\requests\api.py", line 55, in get
return request('get', url, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 422, in request
prep = self.prepare_request(req)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 360, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Python27\lib\site-packages\requests\models.py", line 296, in prepare
self.prepare_cookies(cookies)
File "C:\Python27\lib\site-packages\requests\models.py", line 491, in prepare_cookies
cookie_header = get_cookie_header(self._cookies, self)
File "C:\Python27\lib\site-packages\requests\cookies.py", line 134, in get_cookie_header
jar.add_cookie_header(r)
File "C:\Python27\lib\cookielib.py", line 1326, in add_cookie_header
attrs = self._cookie_attrs(cookies)
File "C:\Python27\lib\cookielib.py", line 1285, in _cookie_attrs
self.non_word_re.search(cookie.value) and version > 0):
TypeError: expected string or buffer

不完全确定我做错了什么......

最佳答案

Cookie 应该只是键值对。您还包括了更多内容,您包括了浏览器跟踪 cookie 的所有元数据,控制客户端代码如何返回或访问此类 cookie。

让您的 cookie 只是一个键值对:

cookies = {'__qca': 'P0-SOMEVALUE-SOMEVALUE'}

映射中的其他所有内容都不是发送到服务器的 Cookie header 的一部分。

在这种特定情况下,'id': 1 键值对引发异常,因为 requests 期望它所看到的值 id cookie 是一个字符串,而不是一个整数。

关于Python 请求包含 cookie 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26020254/

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