-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
117 lines (103 loc) · 3.2 KB
/
get_data.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import requests
import ncaa_utils as utils
from tqdm import tqdm
import time
import numpy as np
from bs4 import BeautifulSoup as bs
import re
import pandas as pd
import random
DATA_DIR = "data"
def format_data(soup):
tags = soup.find_all("tr")
splittags = str(tags).split(",")
splittable = [re.sub(r"<.*?>", ",", i) for i in splittags]
splittable = [
x.replace(",\n,", "")
.replace(",,,", ",")
.replace(",,", ",")
.replace("]", "")
.strip()
for x in splittable
]
splittable = [i for i in splittable if i[0].isdigit()]
df = pd.DataFrame([i.split(",") for i in splittable])
print(df)
df[2] = np.where(df[2] == " ", df[2], None)
try:
df = df.dropna().reset_index(drop=True).drop(columns={0, 2, 23})
except Exception as e:
print(e)
df["Rk"] = np.arange(1, df.shape[0] + 1)
columns = [
"Team",
"Seed",
"Conf",
"W-L",
"AdjEM",
"AdjO",
"AdjO_rank",
"AdjD",
"AdjD_rank",
"AdjT",
"AdjT_rank",
"Luck",
"Luck_rank",
"AdjEM",
"AdjEM_rank",
"OppO",
"OppO_rank",
"OppD",
"OppD_rank",
"NCSOS_AdjEM",
"NCSOS_AdjEM_rank",
"Rk",
]
df.columns = columns
return df
if __name__ == "__main__":
headers = {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36"
}
url = "https://kenpom.com/index.php"
with requests.Session() as request:
response = request.get(url, timeout=30, headers=headers, verify=False)
if response.status_code != 200:
print(response.raise_for_status())
soup = bs(response.text, "html.parser")
links = [x.get("href") for x in soup.find_all("a")]
yearlinks = [x for x in links if str(x)[0:13] == "/index.php?y="]
years = [x.replace("/index.php?y=", "") for x in yearlinks]
maindf = pd.DataFrame()
manres = []
for link in tqdm(yearlinks):
print(link)
page = requests.get(url + link, verify=False, headers=headers)
soup = bs(page.text, "html.parser")
df = format_data(soup)
manres.append(df)
time.sleep(random.randint(1, 3))
maindf = pd.concat(manres)
maindf.to_csv(f"{DATA_DIR}/kenpom_data.csv", index=False)
kenpom_url = "https://kenpom.com"
page = requests.get(kenpom_url, verify=False, headers=headers)
soup = bs(page.text, "html.parser")
# THIS YEARS 64 teams
currentdf = format_data(soup)
currentdf.to_csv(f"{DATA_DIR}/kenpom_current_data.csv", index=False)
# CODE TO UPDATE CONFIG
# untested
west, east, south, midwest = utils.get_bracket_new()
import yaml
from yaml import Loader, Dumper
with open("ncaa_config.yml") as f:
config = yaml.load(f, Loader=Loader)
# to do: replace all St or State with St.
# fix brackets in config in which teams are in
config["TEAMS"]["EAST"] = east
config["TEAMS"]["WEST"] = west
config["TEAMS"]["MIDWEST"] = midwest
config["TEAMS"]["SOUTH"] = south
with open("ncaa_config.yml", "w+") as f:
yaml.dump(config, f, Dumper=Dumper)