在使用requests.get(url).text获取到文本后,将文本写入通过open(uri,'w+')打开的文件后,修改文件名后缀为html,打开后所有字符乱码,查阅资料后是因为编码问题,win中的txt默认为ANSI(选择另存为可查看),而从通过爬取网页读取的文本编码方式为utf-8,所以在打开文件的时候需要设置open打开文件的编码方式,具体命令为

file=open(uri,'r+',encoding='utf-8')

再将爬取的文件存入就不会有编码错误了

python爬虫 编码错误 file open修改编码方式


参考资料链接

python中decode和encode的区别

https://blog.csdn.net/qq_34162294/article/details/53727357

解决方法(百度知道)

https://zhidao.baidu.com/question/1823614815658365068.html












相关文章:

  • 2022-01-15
  • 2021-09-18
  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-06-23
猜你喜欢
  • 2022-12-23
  • 2021-06-04
  • 2021-12-04
  • 2022-12-23
  • 2021-11-22
  • 2022-01-17
  • 2022-01-10
相关资源
相似解决方案