Python3请求错误/ Unicode错误

时间:2018-03-30 17:37:38

标签: python python-requests

我在请求中遇到错误,这让我很困惑,我不知道是什么导致它,任何帮助都将非常感激!

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

1 个答案:

答案 0 :(得分:0)

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

payload包含unicode char \ u2026,无法使用'latin-1'

进行编码

示例

当我们用“latin-1”编码“\ u2026”时,它无法编码,但我们可以使用“utf-8”进行编码:

< / p>

>>> 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"))