首页 > 编程语言 > 详细

利用百度车联网提供的天气查询接口用python查询天气信息

时间:2015-03-25 12:17:08      阅读:429      评论:0      收藏:0      [点我收藏+]

(1)程序查询结果图(图中较下的图是百度查询天气的结果)

技术分享

技术分享



(2)http://developer.baidu.com/map/carapi-7.htm 百度车联网接口说明中有天气查询的接口,目前是免费提供的(一天可以查询5000次)

技术分享

下表是接口返回的json数据。(表中##及后内容是为了方便的查看数据填写的)

{‘date‘: ‘2015-03-24‘, ‘error‘: 0,
 ##&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&begin results
‘results‘: 
[
{
‘pm25‘: ‘95‘, 
##////////////////////////////////////////////////////////begin index
‘index‘: 
[
{‘des‘: ‘建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。‘, ‘tipt‘: ‘穿衣指数‘, ‘zs‘: ‘较冷‘, ‘title‘: ‘穿衣‘},
{‘des‘: ‘较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。‘, ‘tipt‘: ‘洗车指数‘, ‘zs‘: ‘较适宜‘, ‘title‘: ‘洗车‘}, 
{‘des‘: ‘天气较好,风稍大,但温度适宜,是个好天气哦。适宜旅游,您可以尽情地享受大自然的无限风光。‘, ‘tipt‘: ‘旅游指数‘, ‘zs‘: ‘适宜‘, ‘title‘: ‘旅游‘}, 
{‘des‘: ‘昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。‘, ‘tipt‘: ‘感冒指数‘, ‘zs‘: ‘较易发‘, ‘title‘: ‘感冒‘}, 
{‘des‘: ‘天气较好,但因风力稍强,户外可选择对风力要求不高的运动,推荐您进行室内运动。‘, ‘tipt‘: ‘运动指数‘, ‘zs‘: ‘较适宜‘, ‘title‘: ‘运动‘}, 
{‘des‘: ‘属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。‘, ‘tipt‘: ‘紫外线强度指数‘, ‘zs‘: ‘中等‘, ‘title‘: ‘紫外线强度‘}
], 
##///////////////////////////////////////////////////////end index


##..........................................................begin weather_data 今天起的4天气候
‘weather_data‘:
 [
 {‘nightPictureUrl‘: ‘http://api.map.baidu.com/images/weather/night/qing.png‘, ‘weather‘: ‘晴‘, ‘temperature‘: ‘3℃‘, ‘date‘: ‘周二 03月24日 
(实时:15℃)‘, ‘wind‘: ‘微风‘, ‘dayPictureUrl‘: ‘http://api.map.baidu.com/images/weather/day/qing.png‘},
 {‘nightPictureUrl‘: ‘http://api.map.baidu.com/images/weather/night/qing.png‘, ‘weather‘: ‘晴‘, ‘temperature‘: ‘18 ~ 6℃‘, 
‘date‘: ‘周三‘, ‘wind‘: ‘南风3-4级‘, ‘dayPictureUrl‘: ‘http://api.map.baidu.com/images/weather/day/qing.png‘},
 {‘nightPictureUrl‘: ‘http://api.map.baidu.com/images/weather/night/qing.png‘, ‘weather‘: ‘晴‘, ‘temperature‘: ‘20 ~ 
8℃‘, ‘date‘: ‘周四‘, ‘wind‘: ‘微风‘, ‘dayPictureUrl‘: ‘http://api.map.baidu.com/images/weather/day/qing.png‘},
 {‘nightPictureUrl‘: ‘http://api.map.baidu.com/images/weather/night/yin.png‘, ‘weather‘: ‘多云转阴‘, ‘temperature‘: ‘22
 ~ 10℃‘, ‘date‘: ‘周五‘, ‘wind‘: ‘南风3-4级‘, ‘dayPictureUrl‘: ‘http://api.map.baidu.com/images/weather/day/duoyun.png‘}
 ],
 ##............................................................end weather_data
 
‘currentCity‘: ‘北京‘}
],
 ##&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&end results


‘status‘: ‘success‘
 }


import json

import urllib.request
import urllib.parse
url0 = 'http://api.map.baidu.com/telematics/v3/weather'
url ='http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey'
url1 ='https://www.python.org/'

###最简单的方式
##response = urllib.request.urlopen(url1)
##buff = response.read()
##html = buff.decode('utf8')
##response.close()#记得关闭
##print(html)

###使用Request的方式,这种方式同样可以用来处理其他URL,例如FTP:
##import urllib.request
##req = urllib.request.Request(url1)
##response = urllib.request.urlopen(req)
##buff = response.read()
###显示
##the_page = buff.decode('utf8')
##response.close()
##print(the_page)

#使用GET请求
#例子中用到百度车联网中提供的天气查询api接口:
#http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey

#http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey
#decode('gb2312')
name1=input('请输入城市:')
#url1 = 'http://api.map.baidu.com/telematics/v3/weather?location='+name1+'&output=json&ak=yourkey'
#print(url1)
data = {'location':name1,
'output':'json',
'ak':'RehcDwqFhr77yNTZVGKPA45U'}#ak后面的值是我在百度上申请的key
url_values = urllib.parse.urlencode(data)
full_url = url0 + '?' + url_values
#print(full_url)
f = urllib.request.urlopen(full_url)
weatherHTML= f.read().decode('utf-8')#读入打开的url
weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
#print(weatherHTML)
#print(weatherJSON)
print('========================')
results = weatherJSON['results']
print("当前城市:",results[0]['currentCity'])
#当天的天气建议
index = results[0]['index']
for each_index in index:
    print(each_index['title'],":",each_index['zs'])

weather_data =results[0]['weather_data']
for each_data in weather_data:
    print("时间:%s  天气:%s 温度:%s 风力:%s" %(each_data['date'],each_data['weather'],each_data['temperature'],each_data['wind']))
    
import os
os.system('pause')



利用百度车联网提供的天气查询接口用python查询天气信息

原文:http://blog.csdn.net/lxz26192/article/details/44618491

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!