python openweather包不工作

时间:2018-04-24 17:29:55

标签: python python-2.7 openweathermap

你好,我试着按照这个manual进行python开放天气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)

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

您需要在“创建客户端”中附加有效的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