Python calls weather query API

URL:

URL of the API Documentation of Gaode Open Platform


API documentation:

insert image description here
insert image description here

insert image description here
insert image description here
insert image description here
insert image description here


API_Key and city code file acquisition:

click here

insert image description here

After registering relevant information, create a new application according to the document of obtaining Key:

insert image description here

Here is the Key we applied for:

insert image description here

Then download the city code form:

insert image description here

The downloaded form is: AMap_adcode_citycode_20210406.xlsx, you can see that the city code of Beijing is 110000.

insert image description here
download link


Code call:

You need to use the requests package in python

The url writing format in the code should be written according to the parameter table

insert image description here
As described in the request parameters, the parameters represented by parameters include required parameters and optional parameters. All parameters are separated by an ampersand character (&). When we build the url, we need to assign each parameter in advance. For example, the parameter key is the Key we applied for, and city is the code of the city.

The original url is:

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

You only need to assign values ​​to each parameter at the back, add it after the question mark, and separate each parameter with &, such as:

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

This part "key={API_KEY}&city={city}&extensions=all&output=JSON" is the parameter we wrote to form a new url, and then use the requests package to get the page information and return it to the 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)

The returned content (json format) is:

{
    
    "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"}]}]}

Parse with online JSON tools :

{
    
    
	"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"
		}]
	}]
}

Then use the dictionary key-value pair query command to get the information we want, such as:

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}")

return:

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

Full code:

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}")

welcome! ! !

Guess you like

Origin blog.csdn.net/Ayu147258/article/details/130235966