python 爬虫报错'gbk' codec can't decode byte 0x8b in position 1: illegal multibyte sequence

def askURL(url):
heads={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
request=urllib.request.Request(url,headers=heads)
html=''
try:
response=urllib.request.urlopen(request)
html=response.read().decode('gbk')
print(html)
except urllib.error.URLError as e:
if hasattr(e,'code'):
print(e.code)
if hasattr(e,'reason'):
print(e.reason)
return html

运行结果及报错内容

UnicodeDecodeError: 'gbk' codec can't decode byte 0x8b in position 1: illegal multibyte sequence

你用gbk解码,可能识别不出,你查一下网站的源码,有没有类似“charset=utf-8”这样的字段。一般都会主动告知编码方式,如果有的话,你就把decode('gbk')改成decode('utf-8')试试。