UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa1 in position 160: invalid start byte
问题描述
在爬虫一个小说网站时,在使用urllib获取request的response的时候,要进行解码,相关语句如下:
html = reponse.read().decode(“utf-8”),
该语句写“utf-8”,在执行时会报如下错误:
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa1 in position 160: invalid start byte
解决问题
将语句:html = reponse.read().decode(“utf-8”)
改为:html = reponse.read().decode(“GBK”)
即:把"utf-8"变为在国标码"GBK"
问题代码如下
def askURL(url):
head = {
"User-Agent": "Mozillacko) Chrome6"}
request = urllib.request.Request(url, headers=head)
html = ""
try:
reponse = urllib.request.urlopen(request)
//就是下面这句报错!!!
//错误原因:decode()里面要放对应的解码格式
html = reponse.read().decode("utf-8")
//对我这次爬虫小说的网页来说,下面这句才是对的:
//html = reponse.read().decode("GBK")
print("html", html)
except urllib.error.URLError as e:
if hasattr(e, "code"):
print(e.code)
if hasattr(e, "reason"):
print(e.reason)
问题分析
出现这种问题绝大部分情况是因为文件不是 UTF8 编码的(例如,可能是 GBK 编码的)。解决方法是改为对应的解码方式。
参考链接:
https://www.cnblogs.com/zhangshitong/p/11281312.html
https://blog.csdn.net/qq_37701443/article/details/84964684
https://blog.csdn.net/wang7807564/article/details/78164855/