由于 pyOWM 是一个更复杂的 API 的包装器,因此可能他们没有实现 Open Weather API 的警报部分。
我尝试浏览他们的文档,甚至他们的代码库,但我找不到任何关于更改的参考。
我建议使用包装器(因为它对于任何与警报无关的东西都更简单)并单独请求警报,如果这可以在您的用例中完成的话。
因此,在检查天气后,您可以执行以下操作:
import requests
# this is what returns your weather data, with the given parameters.
# note that we're excluding everything except alerts so that you only receive those
API_ENDPOINT = "https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}".format(your_lat, your_lon, 'current,minutely,hourly,daily', your_api_key)
# we call the endpoint to get alert info only
alert_data = requests.get(API_ENDPOINT)
# now let's check the HTTP status to make sure everything went right:
if alert_data.status_code == 200:
print(alert_data['alerts']
# inside this variable there are all alerts
else:
print('some %s error occurred' % alert_data.status_code)
根据 OWM 规范,alert_data 变量中的警报将具有 this 格式,因此它是一个字典列表:
"alerts": [
{
"sender_name": "NWS Tulsa",
"event": "Heat Advisory",
"start": 1597341600,
"end": 1597366800,
"description": "...HEAT ADVISORY REMAINS IN EFFECT FROM 1 PM THIS AFTERNOON TO\n8 PM CDT THIS EVENING...\n* WHAT...Heat index values of 105 to 109 degrees expected.\n* WHERE...Creek, Okfuskee, Okmulgee, McIntosh, Pittsburg,\nLatimer, Pushmataha, and Choctaw Counties.\n* WHEN...From 1 PM to 8 PM CDT Thursday.\n* IMPACTS...The combination of hot temperatures and high\nhumidity will combine to create a dangerous situation in which\nheat illnesses are possible."
},
...
]