gpt4 book ai didi

Python 天气 API

转载 作者:IT老高 更新时间:2023-10-28 22:03:21 26 4
gpt4 key购买 nike

如何将天气数据导入 Python 程序?

最佳答案

由于 Google 已关闭其天气 API,我建议查看 OpenWeatherMap :

The OpenWeatherMap service provides free weather data and forecast API suitable for any cartographic services like web and smartphones applications. Ideology is inspired by OpenStreetMap and Wikipedia that make information free and available for everybody. OpenWeatherMap provides wide range of weather data such as map with current weather, week forecast, precipitation, wind, clouds, data from weather Stations and many others. Weather data is received from global Meteorological broadcast services and more than 40 000 weather stations.

它不是 Python 库,但 super 好用,因为你可以得到 JSON 格式的结果。

这是一个使用 Requests 的示例:

>>> from pprint import pprint
>>> import requests
>>> r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London&APPID={APIKEY}')
>>> pprint(r.json())
{u'base': u'cmc stations',
u'clouds': {u'all': 68},
u'cod': 200,
u'coord': {u'lat': 51.50853, u'lon': -0.12574},
u'dt': 1383907026,
u'id': 2643743,
u'main': {u'grnd_level': 1007.77,
u'humidity': 97,
u'pressure': 1007.77,
u'sea_level': 1017.97,
u'temp': 282.241,
u'temp_max': 282.241,
u'temp_min': 282.241},
u'name': u'London',
u'sys': {u'country': u'GB', u'sunrise': 1383894458, u'sunset': 1383927657},
u'weather': [{u'description': u'broken clouds',
u'icon': u'04d',
u'id': 803,
u'main': u'Clouds'}],
u'wind': {u'deg': 158.5, u'speed': 2.36}}

这是一个使用 PyOWM 的示例,围绕 OpenWeatherMap Web API 的 Python 包装器:

>>> import pyowm
>>> owm = pyowm.OWM()
>>> observation = owm.weather_at_place('London,uk')
>>> w = observation.get_weather()
>>> w.get_wind()
{u'speed': 3.1, u'deg': 220}
>>> w.get_humidity()
76

官方 API 文档可用 here .

要获取 API key 注册以打开天气图 here

关于Python 天气 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1474489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com