UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence

时间:2021-05-30 08:38:56
 import urllib.request
def load_baidu():
url = "https://www.baidu.com/"
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3650.400 QQBrowser/10.4.3341.400",
"haha":"hehe"} #长久请求对象
request = urllib.request.Request(url,headers = header)
#请求网络数据
response = urllib.request.urlopen(request)
print(response)
data = response.read().decode("utf-8") #响应头
# print(response.headers)
#获取请求头的信息(所有头的信息)
request_headers = request.headers
print(request_headers)
with open("headers.html","w")as f:
f.write(data,)
load_baidu()

以上代码出现“UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence”,应该是with里面的编码没有指定编码格式报错的。所以指定一下编码格式为utf-8j就行

UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence

with open("headers.html","w",encoding="utf-8")as f:

UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence