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

Posted baker95935

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.常用的中文编码名称

技术分享

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







以上是关于UnicodeEncodeError: 'gbk' codec can't encode character 'xbb' in position的主要内容,如果未能解决你的问题,请参考以下文章

UnicodeEncodeError: 'ascii' codec can't encode character u'u5929' in position 2(

UnicodeEncodeError: 'ascii' codec can't encode characters

UnicodeEncodeError: 'ascii' codec can't encode characters in

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

UnicodeEncodeError: 'ascii' codec can't encode character...的解决方法

UnicodeEncodeError: 'gbk' codec can't encode character 'u25aa' in position 15: (