Qt实现天气预报与PM2.5监测系统(5)天气预报接口
2017-04-05 11:39
399 查看
Qt实现天气预报与PM2.5监测系统(5)天气预报接口
天气预报接口
www.weather.com.cn API接口数据失效,只是测试数据。sina API 免费 只有2天的 0(今天) 1(明天)
http://php.weather.sina.com.cn/xml.php?city=%CE%E4%BA%BA&password=DJOYnieT8234jlsK&day=0
天气网 API 免费
http://i.tianqi.com/index.php?c=code&id=2&icon=1&py=wuhan&num=6
阿里云API 收费
https://market.aliyun.com/products/57096001/cmapi011242.html?spm=5176.730006-cmapi010812.102.18.yEyQCW#sku=yuncode524200004
阿里云市场
现在免费的接口使用上都有一些不方便的地方,好在收费的服务稳定,数据更完善,阿里云市场中API 收费(0元/100次, 1元/2000次,一年期限)。例如一个设备每天访问5次,一年约2000次,使用费用1元。
天气数据的处理流程
Created with Raphaël 2.1.0天气API操作流程python程序通过网络API接口接收json数据python程序读取需要的字段,保存在文本文件QT程序读取文本文件中数据,进行解析处理后续操作API接口使用
城市CODE code列表
101010100,北京
101020100,上海,上海,上海
101030100,天津,天津,天津
101200101,武汉,武汉,湖北
python程序
#!/usr/bin/python2 import urllib2,urllib #coding=utf-8 import sys ,json #import urllib.request reload(sys) sys.setdefaultencoding('utf-8') #后面实现设置城市功能 ''' codef = open('/home/fa/code_file','r') codecity = codef.read(); codef.close() print(codecity) ''' host = 'http://jisutianqi.market.alicloudapi.com' path = '/weather/query' method = 'GET' appcode = '**你购买的天气appcode**' querys = 'citycode=101010100&cityid=cityid&ip=ip&location=location' #接口中使用系统设置的城市 #querys = 'citycode=' + codecity[0:9] + '&cityid=cityid&ip=ip&location=location' bodys = {} url = host + path + '?' + querys print(querys) request = urllib2.Request(url) request.add_header('Authorization', 'APPCODE ' + appcode) response = urllib2.urlopen(request) content = response.read() if (content): print(content) print("==========================================") print(type(content)) print("==========================================") json_to_python = json.loads(content) print(type(json_to_python)) print("==========================================") ret=json_to_python output=open('day_file','w') output.write(ret['result']['city']+'$') output.write(ret['result']['date']+'$') output.write(ret['result']['week']+'$') output.write(ret['result']['weather']+'$') output.write(ret['result']['temp']+'$') output.write(ret['result']['templow']+'$') output.write(ret['result']['temphigh']+'$') output.write(ret['result']['img']+'$') output.write(ret['result']['humidity']+'$') output.write(ret['result']['winddirect']+'$') output.write(ret['result']['windpower']+'$') output.write(ret['result']['updatetime']+'$') output.write(ret['result']['aqi']['pm10']+'$') output.write(ret['result']['aqi']['pm2_5']+'$') output.write(ret['result']['aqi']['quality']+'$') output.close weekout=open('week_file','w') for x in range(6): weekout.write(ret['result']['daily'][x]['date']+'$') weekout.write(ret['result']['daily'][x]['week']+'$') weekout.write(ret['result']['daily'][x]['night']['templow']+'$') weekout.write(ret['result']['daily'][x]['day']['temphigh']+'$') weekout.write(ret['result']['daily'][x]['day']['weather']+'$') weekout.write(ret['result']['daily'][x]['day']['img']+'$') weekout.write(ret['result']['daily'][x]['day']['winddirect']+'$') weekout.write(ret['result']['daily'][x]['day']['windpower']+'$') weekout.close
python程序运行
会在/home/fa目录下生成,day_file与week_file文件
root@FriendlyARM:/home/fa# ./temp_p2_7.py 101200101 citycode=101200101&cityid=cityid&ip=ip&location=location {"status":"0","msg":"ok","result":{"city":"武汉","cityid":"179","citycode":"101200101","date":"2017-04-05","week":"星期三","weather":"小雨","temp":"15","temphigh":"20","templow":"15","img":"7","humidity":"98","pressure":"1013","windspeed":"11.0","winddirect":"北风","windpower":"2级","updatetime":"2017-04-05 09:36:04","index":[{"iname":"空调指数","ivalue":"较少开启","detail":"您将感到很舒适,一般不需要开启空调。"}, ... "img":"3"}]}} ========================================== <type 'str'> ========================================== <type 'dict'> ========================================== ==================list-all=====================
day_file文件内容
root@FriendlyARM:/home/fa# cat day_file 武汉$2017-04-05$星期三$小雨$15$15$20$7$98$北风$2级$2017-04-05 09:36:04$100$81$轻度污染$108$
week_file文件内容
root@FriendlyARM:/home/fa# cat week_file 2017-04-05$星期三$15$20$小雨$7$东北风$微风$2017-04-06$星期四$15$19$大雨$9$北风$3-4 级$2017-04-07$星期五$15$24$多云$1$东北风$微风$2017-04-08$星期六$14$22$中雨$8$东风$微风$2017-04-09$星期日$12$20$小雨$7$东北风$微风$2017-04-10$星期一$9$13$阵雨$3$$微风$
相关文章推荐
- Qt实现天气预报与PM2.5监测系统(8)一周天气
- Qt实现天气预报与PM2.5监测系统(9)空气质量
- Qt实现天气预报与PM2.5监测系统(7)今日天气
- Qt实现天气预报与PM2.5监测系统(1)项目介绍
- Qt实现天气预报与PM2.5监测系统(3)PM2.5模块程序
- Qt实现天气预报与PM2.5监测系统(10)系统设置
- Qt实现天气预报与PM2.5监测系统(2)Qt运行环境
- Qt实现天气预报与PM2.5监测系统(6)系统界面设计
- Qt实现天气预报与PM2.5监测系统(4)DHT11模块驱动
- 利用中国天气预报接口实现简单天气预报
- Qt实现简易天气预报(解析的是json数据)
- 利用中国天气预报接口实现简单天气预报
- 天气预报的功能实现(使用聚合的提供的外部接口)
- 城市天气三小时预报,天气预报接口实现
- WebService:新浪网天气预报接口实现
- 天气预报简单接口的实现
- 用百度天气接口的实现安卓天气预报(json数据)
- java 实现天气预报 用的是新浪的接口
- Flex 调用WEBService 接口实现天气预报功能.
- XMLHttpRequest异步调用实现调用新浪天气预报页面