编码问题1

UnicodeEncodeError: 'gbk' codec can't encode character 'xbb' in position 0: illegal multibyte seque

html = response.read()
str_html = html.decode()
f = open('baidu.html', 'w+')
for values in str_html:
f.write(values)
f.close()

上面代码编译的时候出现了:UnicodeEncodeError: 'gbk' codec can't encode character 'xbb' in position 0: illegal multibyte seque
对比了一下,应该f的编码格式是GBK的,但是其它的是UTF-8的。所以指定一下编码格式即可。。

f = open('baidu.html', 'w+',encoding='utf-8')


原文:https://blog.csdn.net/nemo2011/article/details/50925418 

原文地址:https://www.cnblogs.com/wisir/p/11106503.html