Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - How to fix "latin-1 codec can't encode characters in position" in requests

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...