设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Python 第44课】 查天气(2)

  [复制链接]

0

主题

0

好友

44

积分

新手上路

Rank: 1

楼主
发表于 2017-7-2 12:26:02 |显示全部楼层
python3.3版本,网址请求和gzip解压方式与python2版本不同,还有最初的天气接口已无更新,
可用接口:http://wthrcdn.etouch.cn/weather_mini?city=北京
通过gzip解压后,#_*_coding:cp936_*_这个编码问题就可以得到解决,亲测window系统同样可以使用utf-8,但没搞清楚原理。以下是python3.3查天气代码:

#_*_coding:cp936_*_
import urllib.request
from city import city
import gzip #调用gzip
import json

exit = False

while not exit:
    cityname = input('你想查询哪个城市的天气?输入q退出\n')
    if cityname == 'q' or cityname == 'Q':
        print('退出!')
        exit = True
    else:
        citycode = city.get(cityname)
        if citycode:
            url = ("http://wthrcdn.etouch.cn/weather_mini?citykey=%s" %citycode)
            request = urllib.request.Request(url)
            response = urllib.request.urlopen(request)
            content = response.read()
            content = gzip.decompress(content).decode('utf-8')#对数据进行解码
            print(content)
         
回复

使用道具 举报

0

主题

0

好友

44

积分

新手上路

Rank: 1

沙发
发表于 2017-7-2 19:48:13 |显示全部楼层
crossin先生 发表于 2017-7-2 15:21
python3 文字比较的时候已经不会受限于编码,都转成unicode了

哦哦,谢谢老师!
回复

使用道具 举报

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

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

GMT+8, 2024-5-20 04:41 , Processed in 0.016921 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部