天天看點

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 167

在用urllib.request庫的時候一部小心就會碰到

url = "http://money.163.com/special/pinglun/"
    data_byte = urllib.request.urlopen(url).read()
    data = data_byte.decode('UTF-8')
    print(data)
           

報錯:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 167: invalid continuation byte
           

這是由于byte字元組沒解碼好,要加另外一個參數

官方文檔中也這麼寫到

bytearray.decode(encoding=”utf-8”, errors=”strict”)

Return a string decoded from the given bytes. Default encoding is 'utf-8'. errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace' and any other name registered via codecs.register_error(), see section Error Handlers. For a list of possible encodings, see section Standard Encodings.
           

意思就是字元數組解碼成一個utf-8的字元串,可能被設定成不同的處理方案,預設是‘嚴格’的,有可能抛出UnicodeError,可以改成‘ignore’,’replace’就能解決

我把上面的data_byte.decode(‘UTF-8’)改成data_byte.decode(‘UTF-8’,‘ignore’)就解決了