Python的天气API [英] Python Weather API

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

问题描述

如何导入气象数据到一个Python程序?

How do I import weather data into a Python program?

推荐答案

由于谷歌已经关闭了其API的天气,我建议检查出的 OpenWeatherMap

Since Google has shut down its weather API, I suggest to check out OpenWeatherMap:

该OpenWeatherMap服务提供免费的气象资料和预报API
  适合喜欢网络和智能手机的任何制图服务
  应用程序。意识形态是由OpenStreetMap的和维基百科的启发
  使信息免费的,可以给大家。 OpenWeatherMap
  提供广泛的气象数据,如地图目前的天气,
  本周预测,precipitation,风,云,从气象站的数据
  和其他许多人。气象数据是从全球气象接收
  广播业务和超过40 000气象站。

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库,但它的超级好用,因为你可以在JSON格式得到结果。

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


而这里的使用 PyOWM ,围绕OpenWeatherMap网络API一个Python包装的例子:


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


官方的API文档可以这里

要获得API密钥注册开气象图这里

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

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

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