欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 10 解决乱码问题

程序员文章站 2022-07-14 23:38:25
...

1.首先要在pycharm中设置默认的编码为utf-8

UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 10 解决乱码问题

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)

UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 10 解决乱码问题
在浏览器中查看html代码:
UnicodeEncodeError: 'gbk' codec can't encode character '\xe7' in position 10 解决乱码问题

相关标签: python爬虫