使用python遍历指定城市的一周气温


Posted in Python onMarch 31, 2017

处于兴趣,写了一个遍历指定城市五天内的天气预报,并转为华氏度显示。

把城市名字写到一个列表里这样可以方便的添加城市。并附有详细注释

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?key=68e75677978441f6872c1106175b8673&cityname=' #用于和cities里的城市进行字符串拼接
low=0
high=2
for city in cities:
 r = requests.get(url+city) # 最基本的GET请求
 #print(r.status_code)  获取返回状态200是成功
 #print(r.text) 打印解码后的返回数据
 data=json.loads(r.text) #返回的json数据被转换为字典类型
 #print(type(data)) data 的数据类型是字典 所以可以按照字典操作(字典里的列表就按列表操作)
 print(city,'近五天天气预报:')
 for i in range(5):
  week='周'+str(data['result']['weather'][i]['week']) #对字典类型进行逐个拆分 如列表 元组等。
  daylow=gettemp(i,'day',low)
  dlf=getft(daylow)
  dayhigh=gettemp(i,'day',high)
  dhf=getft(dayhigh)
  nightlow=gettemp(i,'night',low)
  nlf=getft(nightlow)
  nighthigh=gettemp(i,'night',high)
  nhf=getft(nighthigh)
  print(week,'白天气温:',daylow,'~',dayhigh,'摄氏度','晚上气温:',nightlow,'~',nighthigh,'摄氏度')
  print(' ','白天气温:',dlf,'~',dhf,'华氏度','晚上气温:',nlf,'~',nhf,'华氏度')
 print('\n')

{"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'}]
接着取元组里的字典,逐步拆分即可获得想要的数据。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Python 相关文章推荐
Python 专题一 函数的基础知识
Mar 16 Python
Python3中的列表,元组,字典,字符串相关知识小结
Nov 10 Python
详解python中的 is 操作符
Dec 26 Python
一行代码让 Python 的运行速度提高100倍
Oct 08 Python
Python实现KNN(K-近邻)算法的示例代码
Mar 05 Python
通过pycharm使用git的步骤(图文详解)
Jun 13 Python
Python之数据序列化(json、pickle、shelve)详解
Aug 30 Python
numpy实现神经网络反向传播算法的步骤
Dec 24 Python
Python 中@property的用法详解
Jan 15 Python
tensorflow多维张量计算实例
Feb 11 Python
使用python实现多维数据降维操作
Feb 24 Python
python正则表达式re.search()的基本使用教程
May 21 Python
python网络编程调用recv函数完整接收数据的三种方法
Mar 31 #Python
Python爬取网易云音乐热门评论
Mar 31 #Python
Python中Django发送带图片和附件的邮件
Mar 31 #Python
使用Python对Access读写操作
Mar 30 #Python
使用Python对Excel进行读写操作
Mar 30 #Python
浅述python中argsort()函数的实例用法
Mar 30 #Python
Windows下安装python2和python3多版本教程
Mar 30 #Python
You might like
常见的PHP五种设计模式小结
2011/03/23 PHP
初识PHP
2014/09/28 PHP
php结合安卓客户端实现查询交互实例
2015/05/05 PHP
PHP如何通过date() 函数格式化显示时间
2020/11/13 PHP
php的lavarel框架中join和orWhere的用法
2020/12/28 PHP
prototype class详解
2006/09/07 Javascript
Javascript 面向对象(一)(共有方法,私有方法,特权方法)
2012/05/23 Javascript
js调出上下文菜单的实例
2015/12/17 Javascript
Angularjs整合微信UI(weui)
2016/03/15 Javascript
JS中artdialog弹出框控件之提交表单思路详解
2016/04/18 Javascript
js动态获取子复选项并设计全选及提交的实现方法
2016/06/24 Javascript
jquery判断页面网址是否有效的两种方法
2016/12/11 Javascript
Javascript中类式继承和原型式继承的实现方法和区别之处
2017/04/25 Javascript
Javascript中click与blur事件的顺序详析
2017/04/25 Javascript
关于javascript作用域的常见面试题分享
2017/06/18 Javascript
使用vue-cli编写vue插件的方法
2018/02/26 Javascript
详解Node使用Puppeteer完成一次复杂的爬虫
2018/04/18 Javascript
Vue.js实现表格渲染的方法
2018/09/07 Javascript
layDate插件设置开始和结束时间
2018/11/15 Javascript
VUE2.0+ElementUI2.0表格el-table循环动态列渲染的写法详解
2018/11/30 Javascript
微信小程序开发之左右分栏效果的实例代码
2019/05/20 Javascript
微信小程序如何通过用户授权获取手机号(getPhoneNumber)
2020/01/21 Javascript
查找Vue中下标的操作(some和findindex)
2020/08/12 Javascript
vue使用openlayers实现移动点动画
2020/09/24 Javascript
[50:20]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第四局
2016/03/06 DOTA
python机器学习案例教程——K最近邻算法的实现
2017/12/28 Python
Python中协程用法代码详解
2018/02/10 Python
Django中间件实现拦截器的方法
2018/06/01 Python
Python 中包/模块的 `import` 操作代码
2019/04/22 Python
Python3操作YAML文件格式方法解析
2020/04/10 Python
keras绘制acc和loss曲线图实例
2020/06/15 Python
python上下文管理的使用场景实例讲解
2021/03/03 Python
综合办公室主任岗位职责
2014/04/13 职场文书
慰问敬老院活动总结
2014/04/26 职场文书
无传销社区工作方案
2014/05/13 职场文书
Oracle表空间与权限的深入讲解
2021/11/17 Oracle