python3 報錯:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 201: invalid continu

代碼:python

# -*- 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()應傳入與要抓取的網頁一致的編碼。url

相關文章
相關標籤/搜索