-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.py
299 lines (259 loc) · 11.1 KB
/
scraping.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import bisect
import itertools as it
from typing import Iterable
import numpy as np
import requests
from bs4 import BeautifulSoup
from utils import CACHE_EXPIRY_SCRAPING, cache_with_expiry
NBA_TEAMS_538 = { # as used by 538
"ATL",
"BKN",
"BOS",
"CHA",
"CHI",
"CLE",
"DAL",
"DEN",
"DET",
"GS", # NB
"HOU",
"IND",
"LAC",
"LAL",
"MEM",
"MIA",
"MIL",
"MIN",
"NO", # NB
"NY", # NB
"OKC",
"ORL",
"PHI",
"PHX",
"POR",
"SA", # NB
"SAC",
"TOR",
"UTA",
"WSH",
}
LEAGUE_URLS = {
"Bundesliga": "https://projects.fivethirtyeight.com/soccer-predictions/bundesliga/",
"Premier League": "https://projects.fivethirtyeight.com/soccer-predictions/premier-league/",
"La Liga": "https://projects.fivethirtyeight.com/soccer-predictions/la-liga/",
}
LEAGUE_LENGTHS = {
"Bundesliga": 18,
"Premier League": 20,
"La Liga": 20,
}
CUP_URLS = {
"Champions League": "https://projects.fivethirtyeight.com/soccer-predictions/champions-league/",
"Europa League": "https://projects.fivethirtyeight.com/soccer-predictions/europa-league/",
"Conference League": "https://projects.fivethirtyeight.com/soccer-predictions/europa-conference-league/",
"MLS": "https://projects.fivethirtyeight.com/soccer-predictions/mls/",
}
@cache_with_expiry(seconds=CACHE_EXPIRY_SCRAPING, verbose=False) # cache for this amount, otherwise call again
def get_soup(url):
r = requests.get(url)
return BeautifulSoup(r.text, "html.parser")
def seed_1_2_upset_23():
return 1 - (
get_nba_value("DEN", "make_conf_semis")
* get_nba_value("MEM", "make_conf_semis")
* get_nba_value("MIL", "make_conf_semis")
* get_nba_value("BOS", "make_conf_semis")
)
def english_team_in_cl(cup_stage: str):
return get_cup_stage("Manchester City", "Champions League", cup_stage)
def english_team_2_oo_3(includes_all_three=True):
win_cl = english_team_in_cl("win")
win_el = get_cup_stage("Manchester United", "Europa League", "win")
win_co = get_cup_stage("West Ham United", "Conference League", "win")
win_cl_and_el = win_cl * win_el * (1 - win_co)
win_cl_and_co = win_cl * win_co * (1 - win_el)
win_el_and_co = win_el * win_co * (1 - win_cl)
win_exactly_two = win_cl_and_el + win_cl_and_co + win_el_and_co
win_exactly_three = win_cl * win_el * win_co
if includes_all_three:
return win_exactly_two + win_exactly_three
else:
return win_exactly_two
def get_position_distribution_efficient(team_name, league_name):
"""Returns a probability distribution of the team's position in the league."""
assert league_name in LEAGUE_URLS, f"League name must be one of {LEAGUE_URLS.keys()}"
league_soup = get_soup(LEAGUE_URLS[league_name])
team_row = league_soup.find("tr", {"data-str": team_name})
position_dict = get_position_dict_from_team_row(team_row)
p_dist = np.array([position_dict[pos] for pos in sorted(position_dict)])
assert np.isclose(sum(p_dist), 1, atol=1e-4), f"Values in distribution don't sum to 1: {p_dist}"
return p_dist / sum(p_dist)
def analytical_who_higher(dist_hi, dist_lo):
"""Returns the probability that the first team is higher in the table (=lower number) than the second team."""
assert len(dist_hi) == len(dist_lo), f"Distribution lengths must be equal, got {len(dist_hi)} and {len(dist_lo)}"
outcomes = range(len(dist_hi))
higher = sum(dist_hi[i] * dist_lo[j] for i, j in it.product(outcomes, repeat=2) if i < j)
lower = sum(dist_hi[i] * dist_lo[j] for i, j in it.product(outcomes, repeat=2) if j < i)
result = higher / (higher + lower) # ignore ties, they are impossible
return result
def who_higher(team_hi, team_lo, league_name):
# NB: getting the distribution is slow but the simulation is fast, high n is OK (tested 1e6)
dist_hi = get_position_distribution_efficient(team_hi, league_name)
dist_lo = get_position_distribution_efficient(team_lo, league_name)
return analytical_who_higher(dist_hi, dist_lo)
def get_cup_stage(team_name, cup_name, value_name):
"""only tested for champions league"""
value_names = ("make_playoffs", "make_round_one", "last_sixteen", "quarters", "semis", "finals", "win")
assert value_name in value_names, f"Value name must be one of {value_names}"
assert cup_name in CUP_URLS, f"Cup name must be one of {CUP_URLS.keys()}"
value_map = {
"Champions League": {
"last_sixteen": "pct border-left champ drop-4",
"quarters": "pct champ-quarters",
"semis": "pct champ",
"finals": "pct champ drop-7",
"win": "pct champ champ-win",
},
"Europa League": {
"knockout": "pct border-left champ drop-3",
"last_sixteen": "pct champ drop-4",
"quarters": "pct champ-quarters",
"semis": "pct champ",
"finals": "pct champ drop-7",
"win": "pct champ champ-win",
},
"Conference League": {
# "knockout": "pct border-left champ drop-3",
# "last_sixteen": "pct champ drop-4",
# "quarters": "pct champ-quarters", # untested
"semis": "pct champ",
"finals": "pct champ drop-7",
"win": "pct champ champ-win",
},
"MLS": {
"make_playoffs": "pct mls",
"make_round_one": "pct drop-5 mls",
"win": "pct mls",
},
}
soup = get_soup(CUP_URLS[cup_name])
team_row = soup.find("tr", {"data-str": team_name})
val_to_look_up = value_map[cup_name][value_name]
if cup_name == "MLS" and value_name == "make_playoffs":
val = float(team_row.find_all("td", {"class": val_to_look_up})[0]["data-val"])
elif cup_name == "MLS" and value_name == "win":
val = float(team_row.find_all("td", {"class": val_to_look_up})[-1]["data-val"])
else:
val = float(team_row.find("td", {"class": val_to_look_up})["data-val"])
assert 0 <= val <= 1, f"Value must be between 0 and 1. Received {val}."
return val
def get_league_rel_cl_win(team_name, league_name, value_name):
value_names = ("rel", "cl", "win")
assert value_name in value_names, f"Value name must be one of {value_names}"
assert league_name in LEAGUE_URLS, f"League name must be one of {LEAGUE_URLS.keys()}"
soup = get_soup(LEAGUE_URLS[league_name])
team_row = soup.find("tr", {"data-str": team_name})
rel, cl, win = [float(x["data-val"]) for x in team_row.find_all("td", {"class": "pct"})]
val = locals()[value_name]
assert 0 <= val <= 1, f"Value must be between 0 and 1. Received {val}."
return val
def get_position_dict_from_team_row(team_row):
"""Returns the probability of the team being in the given position."""
position_data = team_row.find("td", {"class": "position-dist"})["data-dist"]
position_dict = {int(p.split(":")[0]): float(p.split(":")[1]) for p in position_data.split()}
return position_dict
def get_league_pos(team_name, league_name, positions):
assert league_name in LEAGUE_URLS, f"League name must be one of {LEAGUE_URLS.keys()}"
soup = get_soup(LEAGUE_URLS[league_name])
# if positions has length 1, convert to list
if not isinstance(positions, Iterable):
positions = [positions]
team_row = soup.find("tr", {"data-str": team_name})
position_dict = get_position_dict_from_team_row(team_row)
# return the sum of the probabilities of the positions we are interested in
val = sum(position_dict[p] for p in positions)
assert 0 <= val <= 1, f"Value must be between 0 and 1. Received {val}."
return val
def find_closest_strings(input_string, string_list):
"""Returns three strings from string_list that are closest to input_string."""
sorted_list_original = sorted(string_list, key=lambda s: s.lower())
string_list = [s.lower() for s in string_list]
sorted_list = sorted(string_list)
i = bisect.bisect_left(sorted_list, input_string.lower())
if i > 0:
if i < len(sorted_list):
return sorted_list_original[i - 1 : i + 2]
else:
return sorted_list_original[-3:]
else:
return sorted_list_original[:3]
def get_nba_value(team_name, value_name):
assert (
team_name in NBA_TEAMS_538
), f"Team name must fit 538 list.\
Received {team_name}; could it be one of {find_closest_strings(team_name, NBA_TEAMS_538)}?"
try:
nba_url = "https://projects.fivethirtyeight.com/2023-nba-predictions"
soup = get_soup(nba_url)
VALUES = ("make_playoffs", "make_conf_semis", "make_conf_finals", "make_finals", "win_finals")
assert value_name in VALUES, f"Value name must be one of {VALUES}"
team_row = soup.find("tr", {"class": None, "data-team": team_name}) # "class": None -> ignores team rows from live games
if team_row is None:
team_row = soup.find("tr", {"class": "eliminated", "data-team": team_name})
td = team_row.find("td", {"data-col": value_name}) # was data-cell until 2023_04_11
text = td.text.strip()
if text == "✓" or text.startswith(">"):
return 1.0
elif text == "—" or text.startswith("<"):
return 0.0
data_val = td["data-val"]
if data_val == "null": # reading from string
return float(text.replace("%", "")) / 100
else:
val = float(data_val) * 1e-12 # * 1e-12 was changed 2023_04_11
# if value_name == "make_playoffs":
# val *= 1
# elif value_name == "make_finals":
# val *= 1e-12 # until shortly before playoffs
# elif value_name == "win_finals":
# val *= 1e-16 # until shortly before playoffs
assert 0 <= val <= 1, f"Value must be between 0 and 1. Received {val}."
return val
except Exception as e:
print(f"Error getting value {value_name} for team {team_name}")
print(e)
return None
def get_nhl_value(team_name, value_name="win"):
assert value_name in (
"make_conf_final",
"make_final",
"win",
), f"Value name must be one of ('make_conf_final', 'make_final', 'win'), got {value_name}"
nhl_url = "https://projects.fivethirtyeight.com/2023-nhl-predictions/"
soup = get_soup(nhl_url)
rows = soup.find_all("tr")
team_row = None
for row in rows:
name_tag = row.find("td", {"class": "name"})
if name_tag is None:
continue
if name_tag.get("data-val") == team_name.lower():
team_row = row
break
if team_row is None:
print("Row not found for team:", team_name)
return None
def get_value_idx(value_name):
if value_name == "make_conf_final":
return -3
elif value_name == "make_final":
return -2
elif value_name == "win":
return -1
value_tag = team_row.find_all("td", {"class": "odds"})[get_value_idx(value_name)]
value = value_tag.get("data-val")
val = float(value)
if val == -1.0: # missed playoffs
val = 0.0
assert 0 <= val <= 1, f"Value must be between 0 and 1. Received {val}."
return val