Qt实现天气预报与PM2.5监测系统(5)天气预报接口

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$$微风$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值