UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 10 解决乱码问题
程序员文章站
2022-07-14 23:38:25
...
1.首先要在pycharm中设置默认的编码为utf-8
2.代码设置编码
r.encoding = 'utf-8-sig'
print(r.text)
3.我的代码:
import requests
url = 'http://www.baidu.com/'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
}
r = requests.get(url, headers=headers)
r.encoding = 'utf-8-sig' #注意要修改编码
print(r.text)
在浏览器中查看html代码:
推荐阅读
-
UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 10 解决乱码问题
-
解决Python3控制台输出的UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\u30fb‘ in position 0:问题
-
UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position ... 问题解决办法之一
-
编码问题:UnicodeEncodeError: 'gbk' codec can't encode character u'\xa0' in position 148:
-
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
-
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
-
PYTHON3解决'gbk' codec can't encode character '\xXX' in position XX