gpt4 book ai didi

python - 如何修复 "latin-1 codec can' t 在请求中的位置编码字符

转载 作者:行者123 更新时间:2023-12-01 07:29:07 24 4
gpt4 key购买 nike

我在 python 3 中编码时遇到问题。当我在我的电脑上测试时,我没有收到错误:

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.

但是当我在我的 VPS 上运行此代码时出现以下错误:

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)

Python 版本是相同的。我不知道发生了什么。

如何修复它?

最佳答案

如果您的环境使用 C os POSIX 语言环境,根据 pep-538,Python 3.7 会自动将其强制转换为支持 UTF-8 的语言环境。

看来您的电脑有 UTF-8C区域设置,而您的 VPS 使用 latin-1 .

尝试在两台计算机上的交互式 Python session 中运行以下命令:

import sys
import locale

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

如果您不想更改 VPS 上的区域设置,您可以设置 PYTHONUTF8=1在其环境中,或者您可以使用 -X utf-8 Python 选项。

关于python - 如何修复 "latin-1 codec can' t 在请求中的位置编码字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57298260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com