import requests # library to interact with HTTP
# Get the data
response = requests.get('https://api.wotblitz.com/wotb/encyclopedia/vehicles/?application_id=e079b7fe335c9af4749be776fbf5fc2b&nation=usa')
# Transform the reponse in python dictionary
data_from_api = response.json()
# Get only the part of data for which we care
tanks = data_from_api.get("data")
tank_names = [] # initialize empty list
# Tanks are now a dictionary as well.
# we want to get all the keys and all the values from them
# and from the values (also dictionaries) we want to extract the name value
for tank, specs in tanks.items():
tank_names.append(specs.get("name"))
print(tank_names)
这里有一个建议,使用“列表理解”从坦克列表中提取名称
这段代码可以做到这一点。由于您是初学者,我将添加一些注释,试图解释代码的作用。 您还可以进一步检查以下概念:
示例代码:
相关问题 更多 >
编程相关推荐