天気予報が動かない…


天気予報が動かない…

Traceback (most recent call last):
  File "c:\Users\athen\Desktop\personal assistant\main.py", line 195, in <module>
    weather = js["main"]
KeyError: 'main'

私が試したこと:

import json
import requests

elif "weather" in query:
                    key = ""
                    weather_url = "https://api.openweathermap.org/data/2.5/weather?"
                    ind = query.split().index("in")
                    location = query.split()[ind +1:]
                    location = "".join(location)
                    url = weather_url + "appid=" + key + "&q=" + location
                    js = requests.get(url).json()
                    if js["cod"] != "404":
                        weather = js["main"]
                        temperature = weather["temp"]
                        temperature = temperature - 273.15
                        humidity = weather["humidity"]
                        desc = js["weather"][0]["description"]
                        weather_response = "the temperature in celcius is " + str(temperature) + "the humidity is" + str(humidity) + "and weather description is " + str(desc)
                        speak = speak + weather_response
                    else:
                        speak = speak + "city not found"

1 解決

ドキュメント[^] ことを示唆している main プロパティが存在する必要があります。 ただし、コードが 200、それだけではありません 404以外 – エラー応答が返される場合があります。

コードをデバッグし、 js 変数を使用して、実際に何が返されているかを確認します。 私たちはあなたのクエリや API キーにアクセスできないため、あなたに代わってそれを行うことはできません。コードをテストするためだけに商用 API にサインアップする人は誰もいません。



Source link

コメント

タイトルとURLをコピーしました