处于兴趣,写了一个遍历指定城市五天内的天气预报,并转为华氏度显示。
把城市名字写到一个列表里这样可以方便的添加城市。并附有详细注释
import requests import json #定义一个函数 避免代码重写多次。 def gettemp(week,d_or_n,date): wendu=data['result']['weather'][week]['info'][d_or_n][date] #对字典进行拆分 return int(wendu) def getft(t): ft=t*1.8+32 return float(str(ft)[0:4]) cities=['保定','北京','上海','武汉','郑州','齐齐哈尔'] #这里可以指定想要遍历的城市 url='http://api.avatardata.cn/Weather/Query"result":{"realtime":{"wind":{"windspeed":null,"direct":"西风","power":"3级","offset":null},"time":"16:00:00","weather":{"humidity":"27","img":"0","info":"晴","temperature":"13"},"dataUptime":"1490517362","date":"2017-03-26","city_code":"101090201","city_name":"保定","week":"0","moon":"二月廿九"},"life":{"date":"2017-3-26","info":{"kongtiao":["开启制暖空调","您将感到有些冷,可以适当开启制暖空调调节室内温度,以免着凉感冒。"],"yundong":["较适宜","天气较好,但考虑风力较强且气温较低,推荐您进行室内运动,若在户外运动注意防风并适当增减衣物。"],"ziwaixian":["中等","属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"],"ganmao":["较易发","昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。"],"xiche":["较适宜","较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"],"wuran":null,"chuanyi":["冷","天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。"]}},"weather":[{"date":"2017-03-26","week":"日","nongli":"二月廿九","info":{"dawn":null,"day":["0","晴","17","西北风","3-4 级","06:12"],"night":["0","晴","2","西南风","微风","18:36"]}},{"date":"2017-03-27","week":"一","nongli":"二月三十","info":{"dawn":["0","晴","2","西南风","微风","18:36"],"day":["0","晴","15","南风","微风","06:11"],"night":["7","小雨","3","南风","微风","18:37"]}},{"date":"2017-03-28","week":"二","nongli":"三月初一","info":{"dawn":["7","小雨","3","南风","微风","18:37"],"day":["1","多云","15","南风","微风","06:09"],"night":["0","晴","3","南风","微风","18:38"]}},{"date":"2017-03-29","week":"三","nongli":"三月初二","info":{"dawn":["0","晴","3","南风","微风","18:38"],"day":["0","晴","18","南风","微风","06:08"],"night":["0","晴","3","北风","微风","18:39"]}},{"date":"2017-03-30","week":"四","nongli":"三月初三","info":{"dawn":["0","晴","3","北风","微风","18:39"],"day":["0","晴","17","北风","微风","06:06"],"night":["0","晴","3","北风","微风","18:40"]}}],"pm25":{"key":"Baoding","show_desc":"0","pm25":{"curPm":"34","pm25":"14","pm10":"26","level":"1","quality":"优","des":"空气很好,可以外出活动"},"dateTime":"2017年03月26日16时","cityName":"保定"},"isForeign":0},"error_code":0,"reason":"Succes"} 这是返回的一个json数据,可以通过json格式化工具查看会方便一些,通过json.loads其实都是字典列表的一些嵌套,而想要取的数据 在字典里"result"里, 而data['result'] 又是一个字典, {'life': {'date': '2017-3-26', 'info': {'yundong': ['较适宜', '天气较好,但考虑风力较强且气温较低,推荐您进行室内运动,若在户外运动注意防风并适当增减衣物。'], 'xiche': ['较适宜', '较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。'], 'ganmao': ['较易发', '昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。'], 'ziwaixian': ['中等', '属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。'], 'chuanyi': ['冷', '天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。'], 'wuran': None, 'kongtiao': ['开启制暖空调', '您将感到有些冷,可以适当开启制暖空调调节室内温度,以免着凉感冒。']}}, 'weather': [{'date': '2017-03-26', 'week': '日', 'info': {'dawn': None, 'night': ['0', '晴', '2', '西南风', '微风', '18:36'], 'day': ['0', '晴', '17', '西北风', '3-4 级', '06:12']}, 'nongli': '二月廿九'}, {'date': '2017-03-27', 'week': '一', 'info': {'dawn': ['0', '晴', '2', '西南风', '微风', '18:36'], 'night': ['7', '小雨', '3', '南风', '微风', '18:37'], 'day': ['0', '晴', '15', '南风', '微风', '06:11']}, 'nongli': '二月三十'}, {'date': '2017-03-28', 'week': '二', 'info': {'dawn': ['7', '小雨', '3', '南风', '微风', '18:37'], 'night': ['0', '晴', '3', '南风', '微风', '18:38'], 'day': ['1', '多云', '15', '南风', '微风', '06:09']}, 'nongli': '三月初一'}, {'date': '2017-03-29', 'week': '三', 'info': {'dawn': ['0', '晴', '3', '南风', '微风', '18:38'], 'night': ['0', '晴', '3', '北风', '微风', '18:39'], 'day': ['0', '晴', '18', '南风', '微风', '06:08']}, 'nongli': '三月初二'}, {'date': '2017-03-30', 'week': '四', 'info': {'dawn': ['0', '晴', '3', '北风', '微风', '18:39'], 'night': ['0', '晴', '3', '北风', '微风', '18:40'], 'day': ['0', '晴', '17', '北风', '微风', '06:06']}, 'nongli': '三月初三'}], 'isForeign': 0, 'pm25': {'pm25': {'des': '空气很好,可以外出活动', 'curPm': '34', 'level': '1', 'pm10': '26', 'pm25': '14', 'quality': '优'}, 'show_desc': '0', 'key': 'Baoding', 'dateTime': '2017年03月26日16时', 'cityName': '保定'}, 'realtime': {'city_name': '保定', 'weather': {'info': '晴', 'img': '0', 'humidity': '27', 'temperature': '13'}, 'week': '0', 'wind': {'windspeed': None, 'power': '3级', 'offset': None, 'direct': '西风'}, 'city_code': '101090201', 'date': '2017-03-26', 'dataUptime': '1490517362', 'time': '16:00:00', 'moon': '二月廿九'}} 相同的方法取 data['result']['weather'] 这又是一个元组, [{'nongli': '二月廿九', 'info': {'night': ['0', '晴', '2', '西南风', '微风', '18:36'], 'dawn': None, 'day': ['0', '晴', '17', '西北风', '3-4 级', '06:12']}, 'week': '日', 'date': '2017-03-26'}, {'nongli': '二月三十', 'info': {'night': ['7', '小雨', '3', '南风', '微风', '18:37'], 'dawn': ['0', '晴', '2', '西南风', '微风', '18:36'], 'day': ['0', '晴', '15', '南风', '微风', '06:11']}, 'week': '一', 'date': '2017-03-27'}, {'nongli': '三月初一', 'info': {'night': ['0', '晴', '3', '南风', '微风', '18:38'], 'dawn': ['7', '小雨', '3', '南风', '微风', '18:37'], 'day': ['1', '多云', '15', '南风', '微风', '06:09']}, 'week': '二', 'date': '2017-03-28'}, {'nongli': '三月初二', 'info': {'night': ['0', '晴', '3', '北风', '微风', '18:39'], 'dawn': ['0', '晴', '3', '南风', '微风', '18:38'], 'day': ['0', '晴', '18', '南风', '微风', '06:08']}, 'week': '三', 'date': '2017-03-29'}, {'nongli': '三月初三', 'info': {'night': ['0', '晴', '3', '北风', '微风', '18:40'], 'dawn': ['0', '晴', '3', '北风', '微风', '18:39'], 'day': ['0', '晴', '17', '北风', '微风', '06:06']}, 'week': '四', 'date': '2017-03-30'}] 接着取元组里的字典,逐步拆分即可获得想要的数据。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月19日
2024年11月19日
- 好薇2024《兵哥哥》1:124K黄金母盘[WAV+CUE]
- 胡歌.2006-珍惜(EP)【步升大风】【FLAC分轨】
- 洪荣宏.2014-拼乎自己看【华特】【WAV+CUE】
- 伊能静.1999-从脆弱到勇敢1987-1996精选2CD【华纳】【WAV+CUE】
- 刘亮鹭《汽车DJ玩主》[WAV+CUE][1.1G]
- 张杰《最接近天堂的地方》天娱传媒[WAV+CUE][1.1G]
- 群星《2022年度发烧天碟》无损黑胶碟 2CD[WAV+CUE][1.4G]
- 罗文1983-罗文甄妮-射雕英雄传(纯银AMCD)[WAV+CUE]
- 群星《亚洲故事香港纯弦》雨果UPMAGCD2024[低速原抓WAV+CUE]
- 群星《经典咏流传》限量1:1母盘直刻[低速原抓WAV+CUE]
- 庾澄庆1993《老实情歌》福茂唱片[WAV+CUE][1G]
- 许巍《在别处》美卡首版[WAV+CUE][1G]
- 林子祥《单手拍掌》华纳香港版[WAV+CUE][1G]
- 郑秀文.1997-我们的主题曲【华纳】【WAV+CUE】
- 群星.2001-生命因爱动听电影原创音乐AVCD【MEDIA】【WAV+CUE】