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

时间:2021-11-28 20:19:28

        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')