天氣API的調用

API store:http://www.avatardata.cn/

免費API自己申請,不需要身份證驗證

貼代碼

import requests
import json

wea_api_url = "http://api.avatardata.cn/Weather/Query"
my_wea_key = 你的密鑰'

def init_wea_url(my_key,city_name):
	url =  wea_api_url + "?key=%s&"%my_key + "cityname=%s"%city_name
	return url

def one_step_of_day(data):
	wea = data[1]
	tmp = data[2]
	wind = data[3] + data[4]
	sun = data[5]
	return wea,tmp,wind,sun

def print_day_info(data):
	date = data['date']
	week = data['week']
	moon = data['nongli']
	dawn = data['info']['dawn']
	dawn_wea,dawn_tmp,dawn_wind,dawn_sun = one_step_of_day(dawn)
	day = data['info']['day']
	day_wea,day_tmp,day_wind,day_sun = one_step_of_day(day)
	night = data['info']['night']
	night_wea,night_tmp,night_wind,night_sun = one_step_of_day(night)
	print("日期:%s"%date)
	print("星期:%s"%week)
	print("農曆:%s"%moon)
	print("凌晨:天氣:%s,溫度:%s,風力風向:%s,日出時間:%s"%(dawn_wea,dawn_tmp,dawn_wind,day_sun))
	print("白天:天氣:%s,溫度:%s,風力風向:%s"%(day_wea,day_tmp,day_wind))
	print("夜間:天氣:%s,溫度:%s,風力風向:%s,日落時間:%s"%(night_wea,night_tmp,night_wind,night_sun))
	print("==========================================================")
	
def wea_main():
	choice = input('''1.查看今日天氣
2.查看未來5天天氣\n''')
	print("============================")
	city = input("輸入你要查看的城市:")
	print("============================")
	request_url = init_wea_url(my_wea_key,city)
	response = requests.get(request_url).content
	content = str(response,encoding = "utf-8")
	total_result = json.loads(content)['result']
	if choice == '1':
		today = total_result['realtime']
		date_time = today['date'] + " " + today['time']
		moon_day = today['moon']
		city = today['city_name']
		wind = today['wind']['direct'] + today['wind']['power']
		tmp = today['weather']['temperature']
		info = today['weather']['info']
		#獲取生活信息
		life = total_result['life']
		kong_tiao = life['info']['kongtiao'][0] + life['info']['kongtiao'][1]
		yun_dong = life['info']['yundong'][0] + life['info']['yundong'][1]
		zi_wai_xian = life['info']['ziwaixian'][0] + life['info']['ziwaixian'][1]
		gan_mao = life['info']['ganmao'][0] + life['info']['ganmao'][1]
		wash_car = life['info']['xiche'][0] + life['info']['xiche'][1]
		wear_clothes = life['info']['chuanyi'][0] + life['info']['chuanyi'][1]
		#輸出
		print("當前時間:%s"%date_time)
		print("陽曆日期:%s"%moon_day)
		print("查看城市:%s"%city)
		print("當前溫度:%s"%tmp)
		print("當前天氣:%s"%info)
		print("風向風力:%s"%wind)
		if total_result['pm25']:
			#獲取pm2.5信息
			pm25 = total_result['pm25']
			pm10 = pm25['pm25']['pm10']
			pm2_5 = pm25['pm25']['pm25']
			quality = pm25['pm25']['quality']
			cur_pm = pm25['pm25']['curPm']
			level = pm25['pm25']['level']
			describe = pm25['pm25']['des']
			print("============================")
			print("當前PM值:%s"%cur_pm)
			print("PM2.5 值:%s"%pm2_5)
			print("PM10的值:%s"%pm10)
			print("空氣評級:%s"%level)
			print("空氣質量:%s"%quality)
			print("綜合概述:%s"%describe)
		print("==========今日推薦==========")
		print("空調:%s"%kong_tiao)
		print("運動:%s"%yun_dong)
		print("紫外線:%s"%zi_wai_xian)
		print("感冒:%s"%gan_mao)
		print("洗車:%s"%wash_car)
		print("穿衣:%s"%wear_clothes)
	elif choice == '2':
		seven_days_weather = total_result['weather']
		for day_weather in seven_days_weather:
			print_day_info(day_weather)	
		
wea_main()
沒有打註釋,但意思基本簡單明瞭。

就是調用API對返回的json結果進行解析,
效果圖:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章