根據內容型別編碼解碼接收的位元組
必須使用正確的字元編碼對接收的位元組進行解碼,以將其解釋為文字:
Python 3.x >= 3.0
import urllib.request
response = urllib.request.urlopen("http://stackoverflow.com/")
data = response.read()
encoding = response.info().get_content_charset()
html = data.decode(encoding)
Python 2.x <= 2.7
import urllib2
response = urllib2.urlopen("http://stackoverflow.com/")
data = response.read()
encoding = response.info().getencoding()
html = data.decode(encoding)