车联网 python_利用百度车联网提供的天气查询接口用python查询天气信息
(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')
原文:http://blog.csdn.net/lxz26192/article/details/44618491
總結
以上是生活随笔為你收集整理的车联网 python_利用百度车联网提供的天气查询接口用python查询天气信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vivo手机怎么保存视频
- 下一篇: 华为微信图标怎么换成别的图标