使用OpenWeatherMap API可以得到401

2024-03-28 16:31:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用JSON获取伦敦的天气数据,但是我得到了HTTPError: HTTP Error 401: Unauthorized。如何使API正常工作?

import urllib2
url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=London&cnt=10&mode=json&units=metric"
response = urllib2.urlopen(url).read()

Tags: 数据importapijsonhttpurlerrorurllib2
1条回答
网友
1楼 · 发布于 2024-03-28 16:31:29

docs通过告诉您需要先注册一个API密钥来打开。

To access the API you need to sign up for an API key

由于您的url不包含密钥,网站会告诉您未经授权。按照说明获取键,然后将其添加到查询参数中。

http://api.openweathermap.org/data/2.5/forecast/daily?APPID=12345&q=...

相关问题 更多 >