如何将天气数据导入到Python程序中?
发布于 2012-10-27 20:47:14
既然谷歌已经关闭了它的天气应用程序接口,我建议你去看看
OpenWeatherMap服务
提供免费的天气数据和预报应用程序接口,适用于任何制图服务,如web和智能手机应用程序。意识形态的灵感来自于OpenStreetMap和维基百科,它们使信息对每个人都是免费和可用的。OpenWeatherMap提供了广泛的天气数据,如当前天气地图,周预报,降水,风,云,气象站数据和许多其他数据。天气数据是从全球气象广播服务和超过40000个气象站接收的。
它不是一个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官方的应用编程接口文档可以在here上找到。
要获得API密钥注册以打开天气图here
https://stackoverflow.com/questions/1474489
复制相似问题