-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_pokedex.py
executable file
·53 lines (47 loc) · 1.82 KB
/
fetch_pokedex.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
52
53
#!/bin/env python3
import json
import logging
import requests
from bs4 import BeautifulSoup as bs
log = logging.getLogger(__name__)
def main(filename="pokedex.json"):
links = ("https://pokemondb.net/go/pokedex", "https://pokemondb.net/go/unavailable")
pokedex = {}
i = 0
for link in links:
log.info(f"Fetching {link}...")
res = requests.get(link)
log.info("Parsing...")
table = bs(res.content, "html.parser").find(id="pokedex").find("tbody")
log.info("Extracting...")
for tr in table.find_all("tr"):
td = tr.find_all("td")
if len(td) > 0:
keep = False
muted = td[1].select_one("small.text-muted")
if muted:
muted = muted.get_text().strip()
if "Forme" in muted:
# exclude " Forme" which is six characters long
muted = td[1].get_text().strip()[:-6]
keep = True
else:
for form in ("Alolan", "Galarian", "Hisuian"):
if form in muted:
keep = True
mon = {
"id": int(td[0].select_one("span:nth-of-type(2)").get_text()),
"name": muted if keep else td[1].get_text().strip(),
"attack": int(td[3].get_text()),
"defense": int(td[4].get_text()),
"hp": int(td[5].get_text()),
}
pokedex[mon["name"].lower()] = mon
log.debug(mon)
i += 1
log.info(f"Fetched data for {i} Pokémons, saving to {filename}...")
with open(filename, "w") as f:
json.dump(pokedex, f, separators=(",", ":"))
return pokedex
if __name__ == "__main__":
main()