前提・実現したいこと
PythonでOpenWeatherMapを使用して明日の天気を表示させたいです。
発生している問題・エラーメッセージ
python
1for name in cities: 2 api2 = "http://api.openweathermap.org/data/2.5/forecast?q={city}&APPID={key}&cnt{cnt}" 3 url2 = api2.format(city=name, key=api_key, cnt=8) 4 r2 = requests.get(api2) 5# 結果はJSON形式なのでデコードする 6 data2 = json.loads(r2.text) 7tenki_nextday = "[明日の天気]",data2["weather"][0]["description"] 8# 9#このtenki_nextdayを出力したいのです。 10#
そして、実行した際に
tenki_nextday = "[明日の天気]",data2["weather"][0]["description"] KeyError: 'weather'
と出てしまいます。
試したこと
api2をurl2に代入せずに直接API取得URLを打ち込む
→結果変わらず
データが取れていないのかと思い、APIURLを参照
→データは取れていました(下記参照)
{"cod":"200","message":0.0056,"cnt":40,"list":[{"dt":1529085600,"main":{"temp":288.44,"temp_min":287.74,"temp_max":288.44,"pressure":1002.56,"sea_level":1023.49,"grnd_level":1002.56,"humidity":99,"temp_kf":0.7},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":56},"wind":{"speed":1.11,"deg":306.001},"rain":{"3h":0.02},"sys":{"pod":"n"},"dt_txt":"2018-06-15 18:00:00"},{"dt":1529096400,"main":{"temp":289.04,"temp_min":288.57,"temp_max":289.04,"pressure":1003.22,"sea_level":1024.15,"grnd_level":1003.22,"humidity":97,"temp_kf":0.47},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":80},"wind":{"speed":1.81,"deg":339.502},"rain":{"3h":0.044999999999999},"sys":{"pod":"d"},"dt_txt":"2018-06-15 21:00:00"},{"dt":1529107200,"main": #投稿文字数制限の為、略
補足情報(FW/ツールのバージョンなど)
Python3.6
Windows 10
ご助力頂ければ幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/16 21:54