1. 程式人生 > >Python 爬蟲解碼問題解決

Python 爬蟲解碼問題解決

import urllib
response = urllib.request.urlopen('http://math.sysu.edu.cn/main/default/index.aspx')
html = response.read()
html = html.decode('utf-8')
print(html)

上述程式碼會出現如下錯誤:

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

問題是解碼錯誤

只需將 html = html.decode('utf-8')

換成 html = html.decode('gbk') 即可