报错 UnicodeEncodeError: gbk codec cant encode character \xa9 in position 3738: illegal multibyt

代码:

import requests
res = requests.get(‘https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html’)
print(res.status_code)
html = res.text
print(html)

报错:
Traceback (most recent call last):
File “c:/Users/jacky/Desktop/test/fb0520.py”, line 6, in
print(html)
UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa9’ in position 3738: illegal multibyte sequence

差了很多搜索都一头雾水。于是一个一个来试。
最终这个方案管用。

import io
import sys
#改变标准输出的默认编码
#utf-8中文乱码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding=‘gb18030’)

改变之后的代码是这样的:
import requests
import io
import sys
res = requests.get(‘https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html’)
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding=‘gb18030’)
html = res.text
print(html)

报错问题解决,具体原来还不是非常明白。以现在的水平,解决问题先。