python 写入文件时UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in pos(

Posted lxai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 写入文件时UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in pos(相关的知识,希望对你有一定的参考价值。

 

正确代码:

import urllib.request

url = "https://blog.csdn.net/john_bian/article/details/71025372?utm_source=itdadao&utm_medium=referral";

response = urllib.request.Request(url=url,method="GET");

result = urllib.request.urlopen(response);

html = result.read().decode("UTF8");

f = open("x.html","w",encoding="UTF8");
f.write(html);
f.close();

  

1:urllib.request.urlopen(url).read(); 读取的内容默认为bytes格式

2:open(filename,open,encode); 打开文件

 

错误提示:

UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\xa0‘ in position 23869: illegal multibyte sequence

  

解决方式:

f = open("x.html","w",encoding="UTF8"); 指定打开文件的编码

或

f = open("x.html","wb")  打开文件为 写入二进制

  

 

对文件操作时 尽量保持所有编码的一致性

以上是关于python 写入文件时UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in pos(的主要内容,如果未能解决你的问题,请参考以下文章