### 備注只是好奇測試了下。
和風天氣官網地址:https://www.heweather.com/
和風天氣接口注冊后可以免費試用。
服務器節點 | 接口地址 | |
---|---|---|
付費用戶 | 中國、北美、歐洲、東南亞 | https://api.heweather.com/v5/ |
免費用戶 | 中國 | https://free-api.heweather.com/v5/ |
# -*- coding: utf-8 -*- import urllib2,json #調用和風天氣的API city可以通過https://cdn.heweather.com/china-city-list.txt城市列表獲取 url = 'https://free-api.heweather.com/v5/weather?city=CN101230201&key=8a439a7e0e034cdcb4122c918f55e5f3' #用urllib2創建一個請求並得到返回結果 req = urllib2.Request(url) resp = urllib2.urlopen(req).read() # print resp # print type(resp) #將JSON轉化為Python的數據結構 json_data = json.loads(resp) city_data=json_data['HeWeather5'][0] hourly_data= json_data['HeWeather5'][0]['hourly_forecast'] daily_data = json_data['HeWeather5'][0]['daily_forecast'] print json_data print u'當前時間:' + daily_data[0]['date'] print u'城市:' + city_data['basic']['city'] print u'PM指數:' + city_data['aqi']['city']['pm25'] print u'白天天氣:' + daily_data[0]['cond']['txt_d'] print u'夜間天氣:' + daily_data[0]['cond']['txt_n'] print u'今天{0}: 氣溫:{1}°/{2}°'.format(str(daily_data[0]['date']),daily_data[0]['tmp']['min'],daily_data[0]['tmp']['max']) print u'未來小時天氣:{0} {1}'.format(str(hourly_data[0]['date']).split()[1],hourly_data[0]['cond']['txt']) print u'未來小時天氣:{0} {1}'.format(str(hourly_data[1]['date']).split()[1],hourly_data[1]['cond']['txt']) print u'未來小時天氣:{0} {1}'.format(str(hourly_data[2]['date']).split()[1],hourly_data[2]['cond']['txt']) print u'未來{0} 天氣:{1}°/{2}°'.format(daily_data[1]['date'],daily_data[1]['tmp']['min'],daily_data[1]['tmp']['max']) print u'未來{0} 天氣:{1}°/{2}°'.format(daily_data[2]['date'],daily_data[1]['tmp']['min'],daily_data[2]['tmp']['max'])
print u'穿衣建議:' + json_data['HeWeather5'][0]['suggestion']['drsg']['txt']
###