代码:

# -*- 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()应传入与要抓取的网页一致的编码。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
猜你喜欢
  • 2021-08-29
  • 2021-10-28
  • 2021-03-31
  • 2021-11-30
  • 2021-07-30
  • 2021-10-22
  • 2022-12-23
相关资源
相似解决方案