阅读背景:

python 3.6 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte错误

来源:互联网 

网站返回的是gzip压缩过的数据,所以要进行解码

# coding=utf-8
from io import BytesIO
import gzip
import urllib.request

url = ('https://wthrcdn.etouch.cn/weather_mini?city=%E4%B8%8A%E6%B5%B7')
resp = urllib.request.urlopen(url)
content = resp.read() # content是压缩过的数据

buff = BytesIO(content) # 把content转为文件对象
f = gzip.GzipFile(fileobj=buff)
res = f.read().decode('utf-8')
print(res)
# coding=utf-8
f



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: