Skip to content

Instantly share code, notes, and snippets.

@ManDeJan
Created February 11, 2023 16:01
Show Gist options
  • Save ManDeJan/5167187d7a33e0f571ae374d30a045dc to your computer and use it in GitHub Desktop.
Save ManDeJan/5167187d7a33e0f571ae374d30a045dc to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup, Tag
import requests
from pathlib import Path
from itertools import chain, combinations
from collections import Counter
file_path = Path('page.html')
if not file_path.is_file():
url = 'https://www.serebii.net/heartgoldsoulsilver/pokewalker-area.shtml'
request = requests.get(url)
with file_path.open('wb') as f:
f.write(request.content)
with file_path.open('rb') as f:
page = f.read()
soup = BeautifulSoup(page, 'html.parser')
# tags = soup.select('.fooinfo:has(b) > a')
tags = [tag.parent.parent for tag in soup.find_all(text='Special Types')]
special_types = []
for tag in tags:
types: list[str] = [child.contents[0] for child in tag.select('u')]
special_types.append(set(types))
counts = Counter(chain.from_iterable(special_types))
unique_types = list(counts.keys())
# print(unique_types, counts)
combs = combinations(unique_types, 5)
possible_typesets = []
for typeset in combs:
if all(map(lambda types: not types.isdisjoint(typeset), special_types)):
possible_typesets.append(typeset)
scored_sets = []
for typeset in possible_typesets:
score = sum(counts[t] for t in typeset)
scored_sets.append((score, sorted(typeset, key=lambda e: counts[e], reverse=True)))
scored_sets.sort(key=lambda e: e[0], reverse=True)
for scored_set in scored_sets:
print(f'{scored_set[0]:2}: {scored_set[1]}')
# Fighting, Electric, Fire, Ground, Dark
# Paras(bug/grass), Sneasel(dark/ice), random fighter
# Machop: Fighting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment