【问题标题】:Python Weather API [closed]Python天气API [关闭]
【发布时间】:2010-12-01 06:18:22
【问题描述】:

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

【问题讨论】:

    标签: python api weather temperature


    【解决方案1】:

    由于谷歌已经关闭了它的天气API,我建议查看OpenWeatherMap

    OpenWeatherMap 服务提供免费的天气数据和预报 API 适用于任何制图服务,如网络和智能手机 应用程序。意识形态的灵感来自 OpenStreetMap 和 Wikipedia 让每个人都能免费获得信息。打开天气地图 提供广泛的天气数据,例如带有当前天气的地图, 每周预报、降水、风、云、气象站数据 和许多其他人。天气数据来自全球气象 广播服务和 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 文档在 here 可用。

    获取API密钥注册打开天气图here

    【讨论】:

    • OpenWeatherMap Web API 资源根据端点使用不同的 JSON blob 进行格式化。所以,解析很糟糕......避免所有这些麻烦,不要通过使用外部库重新发明轮子 - 例如:PyOWM github.com/csparpa/pyowm
    • @csparpa 谢谢,我更新了答案!
    • 嗯很有趣。如何从 {u'speed': 3.1, u'deg': 220} 打印速度。 @paolo
    • @user2763992 速度在dictionaryget_wind 返回。试试print w.get_wind()['speed']
    • api限制请求呢?每天可以完成多少个请求?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2011-01-30
    • 2016-01-27
    • 2011-08-27
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多