-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.py
51 lines (44 loc) · 1.31 KB
/
fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import pandas as pd
import requests
import json
import time
def getPokemonInfo(dex_no):
r = requests.get("https://pokeapi.co/api/v2/pokemon/"+str(dex_no))
mon_dict = r.json()
name = mon_dict['name']
moves = []
abilities = []
types = []
stat_arr = np.zeros(6, dtype=int)
for move in mon_dict['moves']:
moves.append(move['move']['name'])
for i, stat in enumerate(mon_dict['stats']):
stat_arr[i] = stat['base_stat']
for abil in mon_dict['abilities']:
abilities.append(abil['ability']['name'])
for t in mon_dict['types']:
types.append(t['type']['name'])
return name, moves, stat_arr, abilities, types
pokedex = pd.DataFrame({
'name': pd.Series(),
'type': pd.Series(),
'abilities': pd.Series(),
'moves':pd.Series(),
'stats':pd.Series()
})
# There are 1025 pokemon at time of writing.
for dex_no in range(1,1026):
time.sleep(1/10) # polite for the API
print("Fetching mon",dex_no,"/",1025,end='\r')
name, move_list, stats, abils, types = getPokemonInfo(dex_no)
mon = {
'name':name,
'type':types,
'abilities':abils,
'moves':move_list,
'stats':stats,
}
pokedex = pokedex._append(mon, ignore_index=True)
pokedex.to_feather("pokedex.feather")
pokedex.head()