【Python】实现一个天气查询

来源:互联网 发布:android源码语法 编辑:程序博客网 时间:2024/05/21 09:26

实现一个桌面版的天气查询

天气API
首先找获取天气的API:点击查看天气api
可能这篇博客写的有点乱,我来总结一下

http://wthrcdn.etouch.cn/weather_mini?city=北京通过城市名字获得天气数据,json数据http://wthrcdn.etouch.cn/weather_mini?citykey=101010100通过城市id获得天气数据,json数据

具体获取的到的信息,


{“desc”:”OK”,”status”:1000,”data”:{“wendu”:”22”,”ganmao”:”风较大,较易发生感冒,注意防护。”,”forecast”:[{“fengxiang”:”北风”,”fengli”:”5-6级”,”high”:”高温 24℃”,”type”:”晴”,”low”:”低温 11℃”,”date”:”3日星期六”},{“fengxiang”:”北风”,”fengli”:”4-5级”,”high”:”高温 19℃”,”type”:”晴”,”low”:”低温 8℃”,”date”:”4日星期日”},{“fengxiang”:”无持续风向”,”fengli”:”微风”,”high”:”高温 21℃”,”type”:”晴”,”low”:”低温 9℃”,”date”:”5日星期一”},{“fengxiang”:”无持续风向”,”fengli”:”微风”,”high”:”高温 21℃”,”type”:”多云”,”low”:”低温 10℃”,”date”:”6日星期二”},{“fengxiang”:”无持续风向”,”fengli”:”微风”,”high”:”高温 24℃”,”type”:”晴”,”low”:”低温 12℃”,”date”:”7日星期三”},{“fengxiang”:”无持续风向”,”fengli”:”微风”,”high”:”高温 23℃”,”type”:”晴”,”low”:”低温 11℃”,”date”:”8日星期四”}],”yesterday”:{“fl”:”微风”,”fx”:”无持续风向”,”high”:”高温 23℃”,”type”:”晴”,”low”:”低温 12℃”,”date”:”2日星期五”},”aqi”:”59”,”city”:”北京”}}


实现天气查询

  1. 访问url,并读取数据 weather_data = urllib.request.urlopen(url).read()
  2. 解压数据 gzip.decompress(weather_data).decode(‘utf-8’)
  3. 将json数据转化为dict数据 weather_dict = json.loads(weather_data)
  4. 分析提取数据

    主要代码实现

# -*- coding: utf-8 -*-import urllib.requestimport jsonimport gzipcityname = input('你想查询的城市?\n')#访问的url,其中urllib.parse.quote是将城市名转换为url的组件url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(cityname)#发出请求并读取到weather_dataweather_data = urllib.request.urlopen(url).read()#以utf-8的编码方式解压数据weather_data = gzip.decompress(weather_data).decode('utf-8')#将json数据转化为dict数据weather_dict = json.loads(weather_data)print(weather_dict)if weather_dict.get('desc') == 'invilad-citykey':    print("输入的城市名有误")elif weather_dict.get('desc') =='OK' :    forecast = weather_dict.get('data').get('forecast')    startoday = '城市:'+weather_dict.get('data').get('city') +'\n' \              +'日期:'+forecast[0].get('date') + '\n'\              +'温度:'+weather_dict.get('data').get('wendu') + '℃\n' \              +'高温:'+forecast[0].get('high') + '℃\n' \              +'低温: '+forecast[0].get('low') + '℃\n' \              +'风向:'+forecast[0].get('fengxiang') +'\n'\              +'风力:'+forecast[0].get('fengli') + '\n'\              +'天气:'+forecast[0].get('type') + '\n'\              +'感冒:'+weather_dict.get('data').get('ganmao') + '\n'    one_day    ='日期:'+forecast[1].get('date')+'\n' \               +'天气:'+forecast[1].get('type')+'\n'\               +'高温:'+forecast[1].get('high')+'\n'\               +'低温:'+forecast[1].get('low')+'\n'\               +'风向:'+forecast[1].get('fengxiang')+'\n'\               +'风力:'+forecast[1].get('fengli')+'\n'    two_day   = '日期:' + forecast[2].get('date') + '\n' \              + '天气:' + forecast[2].get('type') + '\n' \              + '高温:' + forecast[2].get('high') + '\n' \              + '低温:' + forecast[2].get('low') + '\n' \              + '风向:' + forecast[2].get('fengxiang') + '\n' \              + '风力:' + forecast[2].get('fengli') + '\n'    three_day = '日期:' + forecast[3].get('date') + '\n' \              + '天气:' + forecast[3].get('type') + '\n' \              + '高温:' + forecast[3].get('high') + '\n' \              + '低温:' + forecast[3].get('low') + '\n' \              + '风向:' + forecast[3].get('fengxiang') + '\n' \              + '风力:' + forecast[3].get('fengli') + '\n'    four_day  = '日期:' + forecast[4].get('date') + '\n' \              + '天气:' + forecast[4].get('type') + '\n' \              + '高温:' + forecast[4].get('high') + '\n' \              + '低温:' + forecast[4].get('low') + '\n' \              + '风向:' + forecast[4].get('fengxiang') + '\n' \              + '风力:' + forecast[4].get('fengli') + '\n'    print(one_day)    print(two_day)    print(three_day)    print(four_day)
原创粉丝点击