网址:
高德开放平台
API文档网址
API文档:
API_Key和城市编码文件获取:
点击此处
注册相关信息之后,按照获取Key的文档创建新的应用:
这里是我们申请到的Key:
然后下载城市编码表格:
下载的表格为:AMap_adcode_citycode_0406.xlsx,可以看到,北京的城市编码为110000.
下载链接
代码调用:
需要用到python里的requests包
代码中的url书写格式要按照参数表格去写
如请求参数所描述,parameters代表的参数包括必填参数和可选参数。所有参数均使用和号字符(&)进行分隔。我们在组建url的时候,需要提前将各个参数提前赋值,如,参数key为我们申请的Key,city为城市的编码。
原本的url是:
/v3/weather/weatherInfo?
只需要在后面将各个参数赋值,加在问号后面,各个参数用&分割,如:
url = f"/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 requestsAPI_KEY = "8f2292423c70b8b79e63af30cd84498a"city = "110000"#extensions = "all"url = f"/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":"-04-18 23:08:57","casts":[{"date":"-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":"-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":"-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":"-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": "-04-18 23:08:57","casts": [{"date": "-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": "-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": "-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": "-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}")
返回:
城市: 北京市日期: -04-19白天天气: 多云白天温度: 27
完整代码:
import requestsAPI_KEY = "8f2292423c70b8b79e63af30cd84498a"city = "110000"#extensions = "all"url = f"/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}")
欢迎使用!!!