Python 天气 API

发布于 2024-08-05 06:37:12 字数 27 浏览 4 评论 0原文

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

How do I import weather data into a Python program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-08-12 06:37:12

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

OpenWeatherMap 服务提供免费的天气数据和预报 API
适用于网络和智能手机等任何制图服务
应用程序。 Ideology 的灵感来自 OpenStreetMap 和 Wikipedia
使信息免费并可供所有人使用。打开天气地图
提供广泛的天气数据,例如当前天气的地图,
周预报、降水量、风、云、气象站数据
以及许多其他人。天气数据来自全球气象局
广播服务和超过 40,000 个气象站。

它不是一个 Python 库,但它非常易于使用,因为您可以获得 JSON 格式的结果。

这是使用 请求 的示例:

>>> 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 文档位于此处

要获取 API 密钥,请在此处注册以打开天气地图

Since Google has shut down its weather API, I suggest to check out 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.

It's not a Python library, but it's super easy to use, because you can get results in JSON format.

Here's an example using 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}}

And here's an example using PyOWM, a Python wrapper around the OpenWeatherMap web API:

>>> 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

The official API documentation is available here.

To get the API key sign up to open weather map here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文