python3 报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6
程序员文章站
2022-04-16 08:41:43
代码: 报错: 原因: 确定要抓取的页面的编码,并不是所有网站的编码都是utf-8的,resp.read().decode()应传入与要抓取的网页一致的编码。 ......
代码:
# -*- coding:utf-8 -*- from urllib import request resp = request.urlopen('http://www.xxx.com') print(resp.read().decode('utf-8'))
报错:
Traceback (most recent call last): File "F:/workspace/python/py3/test_urllib.py", line 7, in <module> print(resp.read().decode('utf-8')) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 201: invalid continuation byte
原因:
确定要抓取的页面的编码,并不是所有网站的编码都是utf-8的,resp.read().decode()应传入与要抓取的网页一致的编码。
推荐阅读
-
RLException: Invalid <node> tag: ‘ascii‘ codec can‘t decode byte 0xe6报错
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xcb in position 260: ordinal not in range(128)
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!
-
Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
-
真正解决Windows下UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0错误的方法
-
当在命令行中执行virtualenv venv时报此错误:'utf-8' codec can't decode byte 0xd5 in position 38: invalid continuation by
-
UnicodeDecodeError: 'utf-8' codec can't decode byte in position : invalid continuation byte
-
【debug】UnicodeDecodeError: codec can't decode byte 0xbd in position 4: invalid start byte
-
python3 报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6