python爬取gzip压缩网址的方法

公众号保存 admin 6年前 (2018-02-02) 2324次浏览 0个评论 扫描二维码

# 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


本文标题:python 爬取 gzip 压缩网址的方法
转载请保留页面地址 http://www.kakalong.cn/python%e7%88%ac%e5%8f%96gzip%e5%8e%8b%e7%bc%a9%e7%bd%91%e5%9d%80%e7%9a%84%e6%96%b9%e6%b3%95.html
喜欢 (1)
admin
关于作者:
工作经历: 液晶显示面板厂/ 电视整机厂,从事光学相关工作。 业务爱好编程及各种互联网相关技术。 喜欢结交志同道合的朋友
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址