自分では間違っているところを見つけられませんが、なぜかエラーが出てしまいます。
どなたかご教授ください。
因みに、これはクラッシュロワイヤルというゲームのAPIを使っています。(https://developer.clashroyale.com/#/)
エラーコードは以下になります。
Traceback (most recent call last):
File "C:\Users\mto\Desktop\python_lesson\CRL\crl_api.py", line 53, in <module>
print(battle_info()[0]["type"])
TypeError: string indices must be integers
Python3.72
1import json 2import requests 3 4access_key = #ここでは省略しています 5 6URL = 'https://api.clashroyale.com/v1' 7 8def battle_info(): 9 target_api = URL + "/players/" 10 playerTag = "%238QRCJQ9Y" 11 url = target_api+playerTag+"/battlelog" 12 headers = { 13 "content-type": "application/json; charset=utf-8", 14 "cache-control": "max-age=60", 15 "authorization": "Bearer %s" % access_key} 16 r = requests.get(url,headers=headers) 17 data = r.json() 18 result = json.dumps(data,indent=4) 19 return result 20 21print(battle_info()[0]["type"])
また、print(battle_info)を実行すると、以下のようになります。
#実行コード print(battle_info()) #実行結果 [ { "type": "challenge", "battleTime": "20190509T081821.000Z" #続きは省略
回答1件
あなたの回答
tips
プレビュー