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元。
城市CODE code列表
101010100,北京
101020100,上海,上海,上海
101030100,天津,天津,天津
101200101,武汉,武汉,湖北
#!/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$$微风$