我是一名 Python 新手,尝试使用 Open Weather 的免费 API,但我收到 'cod': '404', 'message': 'Internal error'。我怎样才能解决这个问题?
Posted
技术标签:
【中文标题】我是一名 Python 新手,尝试使用 Open Weather 的免费 API,但我收到 \'cod\': \'404\', \'message\': \'Internal error\'。我怎样才能解决这个问题?【英文标题】:I'm a Python novice trying to use a Free API from Open Weather but I receive 'cod': '404', 'message': 'Internal error'. How can I fix this?我是一名 Python 新手,尝试使用 Open Weather 的免费 API,但我收到 'cod': '404', 'message': 'Internal error'。我怎样才能解决这个问题? 【发布时间】:2021-12-15 00:35:46 【问题描述】:输入城市名称:纽约
'cod': '404', 'message': '内部错误'
找不到城市?
进程以退出代码 0 结束
API 密钥免费且已激活,可能是我使用了错误的 url?
import requests
api_key = 'api_key'
base_url = 'http://api.openweathermap.org/data/2.0/weather?'
city_name = input("Type city name : ")
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
response = requests.get(complete_url)
x = response.json()
print(x)
if x["cod"] == "404":
print("The City Could Not Be Found?")
else:
y = x['main']
current_temperature = y["temp"]
current_pressure = y["pressure"]
current_humidity = y["humidity"]
z = x["weather"]
weather_description = z[0][
"description"]
print("Temperature (in kelvin unit) = " + str(current_temperature) +
"\n atmospheric pressure (in hPa unit) = " + str(
current_pressure) +
"\n humidity (in percentage) = " + str(current_humidity) +
"\n description = " + str(weather_description))
【问题讨论】:
【参考方案1】:尝试首先在浏览器中手动输入所有内容,以测试正确的布局和参数,例如
https://api.openweathermap.org/data/2.5/weather?q=New%20York&appid=your_code_here
"coord":"lon":-74.006,"lat":40.7143,"weather":["id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"],"base":"stations","main":"temp":287.1,"feels_like":286.93,"temp_min":283.32,"temp_max":290.93,"pressure":1003,"humidity":91,"visibility":10000,"wind":"speed":8.75,"deg":50,"gust":11.32,"clouds":"all":90,"dt":1635576402,"sys":"type":1,"id":4610,"country":"US","sunrise":1635593052,"sunset":1635630898,"timezone":-14400,"id":5128581,"name":"New York","cod":200
见:https://openweathermap.org/current
【讨论】:
哇!谢谢,我将 2.0 更改为 2.5,它立即生效。以上是关于我是一名 Python 新手,尝试使用 Open Weather 的免费 API,但我收到 'cod': '404', 'message': 'Internal error'。我怎样才能解决这个问题?的主要内容,如果未能解决你的问题,请参考以下文章
我如何使这个python登录/注册程序工作? (我是python的新手)