-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchampion_gg.py
49 lines (38 loc) · 1.3 KB
/
champion_gg.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
import urllib.request
import bs4
from collections import OrderedDict
# Get Raw HTML-File
req = urllib.request.Request("https://champion.gg/statistics/", headers={'User-Agent': 'Mozilla/5.0'})
page = urllib.request.urlopen(req).read()
# Parse Html
soup = bs4.BeautifulSoup(page, "html5lib")
# Navigate to Champion-Winrate-Data
data: str = soup.body.find(attrs={'class': ['primary-hue']}).find(attrs={'class': ['main-container']}).find(attrs={'class': ['page-content']}) \
.find_all(name="script", recursive=False)[1].string[23:-2]
# Parse and Evaluate String
data = data.replace("null", "None")
data = eval(data)
# Loop trough every Champion
stats = {}
for champion in data:
name = champion["key"]
winRatio = champion["general"]["winPercent"]
# Add ChampionEntry
if name not in stats:
stats[name] = winRatio
# Append to List
else:
if type(stats[name]) is not list:
stats[name] = [stats[name]]
stats[name].append(winRatio)
# Calculate average if necessary
for i in range(len(stats)):
name = list(stats.keys())[i]
if type(stats[name]) is list:
total = sum(stats[name])
stats[name] = total/len(stats[name])
# Decimal Precision
for i in stats:
stats[i] = round(stats[i], 3)
# Sort
stats = dict(OrderedDict(sorted(stats.items())))