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

python requests unicode encode error while attaching cookies

我正在使用 Python 请求访问包含 JSON 数据的 HTTPS 页面。我在 get 请求中传递了 2 个参数,它们在下面列出 验证=假 饼干=饼干 我知道 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,或 JSON 使用转义的 unicode 字符,假设 cookie 的消费者将知道如何解码值。否则,从 cookie 值中删除此类字符。