![](/img/trans.png)
[英]How to get all the Pokémon with the maximum defense using spark RDD operations?
[英]How can i get all the pokémon types automatically with pokeapi?
我是一名初級程序員,正在探索 API 和 python。
我想知道我是否可以像我在這里所做的那樣在不傳遞數字的情況下獲得所有類型的神奇寶貝:
import requests
name = "charizard"
url = f'https://pokeapi.co/api/v2/pokemon/%7Bname%7D'
poke_request = requests.get(url)
poke_request = poke_request.json()
types = poke_request['types'][0]['type']['name']
我嘗試做一些循環或傳遞變量,但我總是以一些“切片”錯誤告終。
如果有一種方法可以打印列表中的類型,那就太好了!
PokeAPI 可以直接訪問 pokemon 類型:
import requests
url = "https://pokeapi.co/api/v2/type/"
poke_request = requests.get(url)
types = poke_request.json()
for typ in types["results"]:
print(typ)
編輯:
您可以使用將名稱保存為列表
names = [typ["name"] for typ in types["results"]]
用戶@JustLearning 幫助了我,我能夠根據我想要的場景調整解決方案:
import requests
url = "https://pokeapi.co/api/v2/pokemon/charizard"
poke_request = requests.get(url)
types = poke_request.json()
names = [typ["type"]["name"] for typ in types["types"]]
print(names)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.