gpt4 book ai didi

附加cookie时python请求unicode编码错误

转载 作者:行者123 更新时间:2023-12-05 03:31:37 29 4
gpt4 key购买 nike

我正在使用 Python 请求访问包含 JSON 数据的 HTTPS 页面。我在 get 请求中传递了 2 个参数,它们在下面列出验证=假 cookies = cookies 我知道 verify = False 是为了通过 SSL 认证验证,而 cookies = cookie 参数是为了传递 cookie 值。实际上我在字典中放入了 2 个 cookie 值,其中一个值很长,可能超过 300 个字符。但是当我运行我的代码时,我得到了这个错误

UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 602: ordinal not in range(256)

我的代码附在下面

import requests
test_url = "https://abc/cde"
cookie = {"cookie1":"value1(10 characters)", "cookie2":"value2(300+ characters)"}
response = requests.get(test_URL, verify=False, cookies= cookie)
print(response.content)

最佳答案

\u2026HORIZONTAL ELLISPSIS 字符,它在 cookie 中不是合法字符(参见 this Q&A 的答案

>>> r = requests.get('https://www.google.com', cookies={'mycookie': '\u2026'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/virtual-envs/so310/lib/python3.10/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/home/me/virtual-envs/so310/lib/python3.10/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
...
File "/usr/local/lib/python3.10/http/client.py", line 1317, in _send_request
self.putheader(hdr, value)
File "/home/me/virtual-envs/so310/lib/python3.10/site-packages/urllib3/connection.py", line 224, in putheader
_HTTPConnection.putheader(self, header, *values)
File "/usr/local/lib/python3.10/http/client.py", line 1249, in putheader
values[i] = one_value.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 9: ordinal not in range(256)

这个问题的通常解决方案是使用不暴露非法字符的方法对值进行编码,例如 base64,或转义了 unicode 字符的 JSON,假设 cookie 的消费者知道如何解码该值.否则,从 cookie 值中删除此类字符。

关于附加cookie时python请求unicode编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70616716/

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