python 写数据入文件碰见的bug:UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘xee‘ in position 21865:

python 写数据入文件碰见的bug:UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘xee‘ in position 21865:,第1张

问题描述

获取网页数据时,避免多次获取被封ip,先将获取的数据给存成文件,发现解码后存入报错了

    data = respon.read().decode("utf-8")
    file = open("data.html", "w")
    file.write(data, encodings = "utf-8")
    file.close()
Traceback (most recent call last):
    file.write(data)
UnicodeEncodeError: 'gbk' codec can't encode character '\xee' in position 21865: illegal multibyte sequence

解决方案:

在setting里边指定编码为utf-8,就可以解决问题了,如下图所示。

后续又报了错误
    TypeError: TextIOWrapper.write() takes no keyword arguments

解决方案:
    file = open("top2.html", "w",encoding='utf-8')
错误
file = open("top2.html", "r")


UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 3301: illegal multibyte sequence

解决方案:
    file = open("top2.html", "r", encoding="utf-8")

记录错误用

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/738337.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存