Python调用天气查询API

网址:

高德开放平台
API文档网址


API文档:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


API_Key和城市编码文件获取:

点击此处

在这里插入图片描述

注册相关信息之后,按照获取Key的文档创建新的应用:

在这里插入图片描述

这里是我们申请到的Key:

在这里插入图片描述

然后下载城市编码表格:

在这里插入图片描述

下载的表格为:AMap_adcode_citycode_20210406.xlsx,可以看到,北京的城市编码为110000.

在这里插入图片描述
下载链接


代码调用:

需要用到python里的requests包

代码中的url书写格式要按照参数表格去写

在这里插入图片描述
如请求参数所描述,parameters代表的参数包括必填参数和可选参数。所有参数均使用和号字符(&)进行分隔。我们在组建url的时候,需要提前将各个参数提前赋值,如,参数key为我们申请的Key,city为城市的编码。

原本的url是:

https://restapi.amap.com/v3/weather/weatherInfo?

只需要在后面将各个参数赋值,加在问号后面,各个参数用&分割,如:

url = f"https://restapi.amap.com/v3/weather/weatherInfo?key={API_KEY}&city={city}&extensions=all&output=JSON"

这部分“key={API_KEY}&city={city}&extensions=all&output=JSON”是我们写入的参数,组成了一个新的url,然后用requests包获取页面信息,返回给response:

import requests

API_KEY = "8f2292423c70b8b79e63af30cd84498a"

city = "110000"

#extensions = "all"

url = f"https://restapi.amap.com/v3/weather/weatherInfo?key={API_KEY}&city={city}&extensions=all&output=JSON"

response = requests.get(url)

data = response.json()

print(data)

返回的内容(json格式)为:

{
    
    "status":"1","count":"1","info":"OK","infocode":"10000","forecasts":[{
    
    "city":"北京市","adcode":"110000","province":"北京","reporttime":"2023-04-18 23:08:57","casts":[{
    
    "date":"2023-04-18","week":"2","dayweather":"晴","nightweather":"晴","daytemp":"27","nighttemp":"9","daywind":"东南","nightwind":"东南","daypower":"≤3","nightpower":"≤3","daytemp_float":"27.0","nighttemp_float":"9.0"},{
    
    "date":"2023-04-19","week":"3","dayweather":"多云","nightweather":"多云","daytemp":"27","nighttemp":"12","daywind":"南","nightwind":"南","daypower":"4","nightpower":"4","daytemp_float":"27.0","nighttemp_float":"12.0"},{
    
    "date":"2023-04-20","week":"4","dayweather":"多云","nightweather":"多云","daytemp":"22","nighttemp":"12","daywind":"东北","nightwind":"东北","daypower":"4","nightpower":"4","daytemp_float":"22.0","nighttemp_float":"12.0"},{
    
    "date":"2023-04-21","week":"5","dayweather":"多云","nightweather":"多云","daytemp":"17","nighttemp":"9","daywind":"西南","nightwind":"西南","daypower":"≤3","nightpower":"≤3","daytemp_float":"17.0","nighttemp_float":"9.0"}]}]}

在线JSON工具进行解析:

{
    
    
	"status": "1",
	"count": "1",
	"info": "OK",
	"infocode": "10000",
	"forecasts": [{
    
    
		"city": "北京市",
		"adcode": "110000",
		"province": "北京",
		"reporttime": "2023-04-18 23:08:57",
		"casts": [{
    
    
			"date": "2023-04-18",
			"week": "2",
			"dayweather": "晴",
			"nightweather": "晴",
			"daytemp": "27",
			"nighttemp": "9",
			"daywind": "东南",
			"nightwind": "东南",
			"daypower": "≤3",
			"nightpower": "≤3",
			"daytemp_float": "27.0",
			"nighttemp_float": "9.0"
		}, {
    
    
			"date": "2023-04-19",
			"week": "3",
			"dayweather": "多云",
			"nightweather": "多云",
			"daytemp": "27",
			"nighttemp": "12",
			"daywind": "南",
			"nightwind": "南",
			"daypower": "4",
			"nightpower": "4",
			"daytemp_float": "27.0",
			"nighttemp_float": "12.0"
		}, {
    
    
			"date": "2023-04-20",
			"week": "4",
			"dayweather": "多云",
			"nightweather": "多云",
			"daytemp": "22",
			"nighttemp": "12",
			"daywind": "东北",
			"nightwind": "东北",
			"daypower": "4",
			"nightpower": "4",
			"daytemp_float": "22.0",
			"nighttemp_float": "12.0"
		}, {
    
    
			"date": "2023-04-21",
			"week": "5",
			"dayweather": "多云",
			"nightweather": "多云",
			"daytemp": "17",
			"nighttemp": "9",
			"daywind": "西南",
			"nightwind": "西南",
			"daypower": "≤3",
			"nightpower": "≤3",
			"daytemp_float": "17.0",
			"nighttemp_float": "9.0"
		}]
	}]
}

然后用字典键值对查询指令,获取我们想要的信息,如:

s_city = data["forecasts"][0]["city"]
s_date = data["forecasts"][0]["casts"][0]["date"]
s_dayweather = data["forecasts"][0]["casts"][0]["dayweather"]
s_daytemp = data["forecasts"][0]["casts"][0]["daytemp"]

print(f"城市: {s_city}")
print(f"日期: {s_date}")
print(f"白天天气: {s_dayweather}")
print(f"白天温度: {s_daytemp}")

返回:

城市: 北京市
日期: 2023-04-19
白天天气: 多云
白天温度: 27

完整代码:

import requests

API_KEY = "8f2292423c70b8b79e63af30cd84498a"

city = "110000"

#extensions = "all"

url = f"https://restapi.amap.com/v3/weather/weatherInfo?key={API_KEY}&city={city}&extensions=all&output=JSON"

response = requests.get(url)

data = response.json()

print(data)

s_city = data["forecasts"][0]["city"]
s_date = data["forecasts"][0]["casts"][0]["date"]
s_dayweather = data["forecasts"][0]["casts"][0]["dayweather"]
s_daytemp = data["forecasts"][0]["casts"][0]["daytemp"]

print(f"城市: {s_city}")
print(f"日期: {s_date}")
print(f"白天天气: {s_dayweather}")
print(f"白天温度: {s_daytemp}")

欢迎使用!!!

猜你喜欢

转载自blog.csdn.net/Ayu147258/article/details/130235966
今日推荐