""" 若是隻用普通的 import urllib.request html = urllib.request.urlopen("http://www.sina.com").read() print(html.decode('gbk')) 出現下面的錯誤 builtins.UnicodeDecodeError: 'gbk' codec can't decode byte 0x8b in position 1: illegal multibyte sequence 怎麼辦?原來是有的網站將網頁用gzip壓縮了 。 請看下面的代碼 import io import urllib.request as r import gzip req = r.Request("http://www.sina.com", headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36", "Accept-Encoding": "gzip"}) bs = r.urlopen(req).read() bi = io.BytesIO(bs) gf = gzip.GzipFile(fileobj=bi, mode="rb") print(gf.read().decode("gbk"))