python爬取gzip压缩网址的方法

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

url = (‘http://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)

 

转自 https://segmentfault.com/q/1010000008631001/a-1020000008631475

链接:https://pan.baidu.com/s/1QLlbjaliWMI1bmhO52il0g

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注