首页 > 解决方案 > python openweather包不起作用

问题描述

你好我尝试按照本手册获取python open weather api

这里是示例 manula 代码:

import openweather
from datetime import datetime

# create client
ow = openweather.OpenWeather()

# find weather stations near me
stations = ow.find_stations_near(
    7.0,  # longitude
    50.0, # latitude
    100   # kilometer radius
)

# iterate results
for station in stations:
    print station

但这不起作用,我收到此错误消息:

OpenWeather.do_request(): No connection. (1. attempt)
OpenWeather.do_request(): No connection. (2. attempt)
OpenWeather.do_request(): No connection. (3. attempt)

知道为什么吗?

标签: pythonpython-2.7openweathermap

解决方案


您需要将有效的 API 密钥附加到“创建客户端”请求。这个包所做的是将您的输入解析为 JSON 请求并返回,因此在您创建客户端时它需要具有 API 密钥,以便将其附加到它发送到 openweathermap 的 URL。

import openweather
from datetime import datetime

# create client
ow = openweather.OpenWeather('3f14d26ebe5502a831e98067ae851b99')

# find weather stations near me
stations = ow.find_stations_near(
    7.0,  # longitude
    50.0, # latitude
    100   # kilometer radius
)

# iterate results
for station in stations:
    print station

推荐阅读