Python3请求错误/Unicode E

2024-03-28 08:45:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在请求中遇到了一个错误,这让我很困惑,我不知道是什么原因造成的,任何帮助都将非常感谢!在

https://pastebin.com/VWYYMPBR(had to use pastebin as it would not let me post the error)


Tags: tohttpscomuseas错误not原因
1条回答
网友
1楼 · 发布于 2024-03-28 08:45:04

问题出在response = session.post(url, data=payload)

负载包含unicode字符\u2026,无法用“latin-1”编码

示例

当我们用“拉丁语-1”编码“\u2026”时,它不能被编码,但我们可以用“utf-8”编码:
>>> a="\u2026"
>>> a.encode('latin-1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 0: ordinal not in range(256)
>>> a.encode('utf-8')
b'\xe2\x80\xa6'

可能的修复方法是

。 用'utf-8编码你的有效载荷

例如response = session.post(url, data=payload.encode("utf-8"))

相关问题 更多 >