标签:python bytes ref 打开文件 url name source 二进制 close
正确代码:
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 position 23869:
标签:python bytes ref 打开文件 url name source 二进制 close
原文地址:https://www.cnblogs.com/lxai/p/8759032.html