如何将天气数据导入 Python 程序?
问问题
99420 次
1 回答
69
由于谷歌已经关闭了它的天气 API,我建议查看OpenWeatherMap:
OpenWeatherMap 服务提供适用于任何制图服务(如网络和智能手机应用程序)的免费天气数据和预报 API。Ideology 受到 OpenStreetMap 和 Wikipedia 的启发,它们使信息免费且可供所有人使用。OpenWeatherMap 提供广泛的天气数据,例如带有当前天气的地图、每周预报、降水、风、云、气象站的数据等等。天气数据来自全球气象广播服务和 40 000 多个气象站。
它不是 Python 库,但非常易于使用,因为您可以获得 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 文档可在此处获得。
要获取 API 密钥,请在此处注册以打开天气图
于 2012-10-27T12:47:13.507 回答