Python天气API [英] Python Weather API

查看:22
本文介绍了Python天气API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

<块引用>

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

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

这是一个使用请求的例子:

<预><代码>>>>从 pprint 导入 pprint>>>进口请求>>>r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London&APPID={APIKEY}')>>>pprint(r.json()){u'base': u'cmc 站',u'clouds': {u'all': 68},你鳕鱼:200,u'coord': {u'lat': 51.50853, u'lon': -0.12574},你'dt':1383907026,u'id': 2643743,u'main':{u'grnd_level':1007.77,你'湿度':97,你'压力':1007.77,u'sea_level':1017.97,你的温度: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 cloud',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 包装器:

<预><代码>>>>导入pyowm>>>owm = pyowm.OWM()>>>观察 = owm.weather_at_place('伦敦,英国')>>>w = 观察.get_weather()>>>w.get_wind(){u'speed':3.1,u'deg':220}>>>w.get_湿度()76

<小时>

官方 API 文档可在此处获得.

要在此处

注册 API 密钥以打开天气地图

How do I import weather data into a Python program?

解决方案

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

这篇关于Python天气API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆