UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

今天学习urllib爬取网页在使用decode出现了错误:
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte

>>> import urllib.request
>>> response = urllib.request.urlopen("http://placekitten.com/g/200/300")
>>> html = response.read()
>>> html = html.decode("utf-8")
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    html = html.decode("utf - 8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

问题出在decode()方法的第二个参数errors默认设置为“strict(严格)”形式造成的,里面含有无法进行转换 “二进制数据”。

猜你喜欢

转载自blog.csdn.net/weixin_42320853/article/details/86490545