Python
1import requests 2 3def weather_command(): 4 base_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' 5 city_code = '130010' 6 url = '{}?city={}'.format(base_url,city_code) 7 r = requests.get(url) 8 print(r.status_code) 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)
pythonでwebサイトから天気情報を取得するプログラムを作っていますがうまくいきません。
以下エラーメッセージが表示されます。
python pybot_weather.py
天気
200
Traceback (most recent call last):
File "pybot_weather.py", line 21, in <module>
response = weather_command()
File "pybot_weather.py", line 11, in weather_command
label = weather_data['forecasts'][0]['dateLabel']
TypeError: 'method' object is not subscriptable
status_codeは200が返ってきているのでjson変換後に
locationとかcityとかのデータが取れていないようですが
原因がよくわかりません。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。