设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 83127|回复: 0
打印 上一主题 下一主题

【Python 第45课】 查天气(3)

[复制链接]

0

主题

0

好友

4

积分

新手上路

Rank: 1

楼主
发表于 2018-1-20 15:35:44 |显示全部楼层
【添砖加瓦】
Python3 调用urllib获取网络资源、gzip解压的方法:
1、与Python2不同,Python3调用的是urllib.request;
2、与Python2不同,Python3调用gzip模块后,直接使用gzip.decompress()函数解压。
代码如下:

from city import city
import urllib.request
import gzip

cityname=input("请输入你想查询天气的城市名称?\n")
citycode=city.get(cityname)


if citycode:
    url=("http://wthrcdn.etouch.cn/weather_mini?citykey=%s"%citycode)
    print(url)
   
    content=urllib.request.urlopen(url).read()
    content=gzip.decompress(content) .decode("utf-8")
    print(content)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-19 07:30 , Processed in 0.026095 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部