そのような場合、weather_dataのみで内容を確認、weather_data['forecasts']で内容を確認、、など個々に確認してみてはいかがですか?
本を見て下記のような天気の情報を取得するプログラムを書いてるのですが
python
1import requests 2 3 4def weather_command(): 5 base_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' 6 city_code = '130010' 7 url = '{}?city={}'.format(base_url, city_code) 8 r = requests.get(url) 9 weather_data = r.json() 10 city = weather_data['location']['city'] 11 label = weather_data['forecasts'][0]['dateLabel'] 12 telop = weather_data['forecasts'][0]['telop'] 13 14 response = '{}ノ{}ノ天気ハ{}デス'.format(city, label, telop) 15 return response 16 17command = input("> ") 18response = "" 19 20if '天気' in command: 21 response = weather_command() 22 23print(response) 24```以下エラー文 25Traceback (most recent call last): 26File "pybot_weather.py", line 45, in <module> 27response = weather_command() 28File "pybot_weather.py", line 11, in weather_command 29label = weather_data['forecasts'][0]['dateLabel'] 30IndexError: list index out of range 31 32となってしまいます. 何回か見直しても本のサンプルコードと同じになっていますし, livedoorのAPIのページを確認しましたが, 33 weather_data['forecasts'][0]['dateLabel'] 34の書き方はまちがっていないとおもうのですが... 35なにがだめなのかさっぱりです 教えてもらえるとうれしいです 36よろしくおねがいします
あなたの回答
tips
プレビュー