UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position

python实现爬虫遇到编码问题:

error:UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

解决办法:改变标准输出

from urllib import request
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #改变标准输出的默认编码
req=request.Request('http://www.baidu.com')
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')
resp=request.urlopen(req)
print(resp.read().decode('UTF-8'))
添加页面中 标注红色的代码即可

ps:

1.str转bytes叫encode,bytes转str叫decode

2.常用的中文编码名称

UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position

参考文章:http://blog.csdn.net/jim7424994/article/details/22675759


上一篇:Android Studio项目整合PullToRefresh的问题记录


下一篇:解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX