How to fix “latin-1 codec can't encode characters in position” in requests

拥有回忆 提交于 2020-12-10 07:44:20

问题


I am having trouble with encoding in python 3. When I was testing on my PC I get no errors:

Python 3.7.3 (default, Jun 24 2019, 04:54:02) 
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> print(requests.get('https://www.kinopoisk.ru').text)

everything good.

But when I ran this code on my VPS a have following error:

Python 3.7.3 (default, Apr  3 2019, 19:16:38) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import requests
>>> print(requests.get('https://www.kinopoisk.ru').text) 

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 393-401: ordinal not in range(256)

The python versions are the same. I don't know what is going on.

How do I fix it?


回答1:


If your environment uses the C os POSIX locale, Python 3.7 automatically coerces that to a UTF-8 aware locale, according to pep-538.

So it seems that your PC has an UTF-8 or C locale set, while your VPS uses latin-1.

Try running the following in an interactive Python session on both machines:

import sys
import locale

print(sys.getfilesystemencoding())
print(locale.getpreferredencoding())

It you do not want to change the locale on your VPS, you could set PYTHONUTF8=1 in its environment, or you could use the -X utf-8 option with Python.



来源:https://stackoverflow.com/questions/57298260/how-to-fix-latin-1-codec-cant-encode-characters-in-position-in-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!