python基于openweather——api实现自动获取天气信息
api获取链接: link
第一步:注册获取api
第二步:代码实现
import requests
import json
api_key="你的api"
lat = 22.3092607
lon = 113.9304907
url_c = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&lang=zh_cn"
response=requests.get(url_c,headers={'Connection':'close','Content-Type': 'charset=utf-8'}).json()
print(response)
第三步:扩展:多种方式获取天气信息
1、按城市名称
参数:
q城市名称和国家/地区代码除以逗号,使用ISO 3166国家/地区代码
API调用的示例:
api.openweathermap.org/data/2.5/weather?q=London
api.openweathermap.org/data/2.5/weather?q=London,uk
2、按城市ID
API调用的示例:
api.openweathermap.org/data/2.5/weather?id=2172797
3、按地理坐标
API调用:
api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}
4、按邮政编码
API调用的示例:
api.openweathermap.org/data/2.5/weather?zip=94040,us