-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
278 lines (260 loc) · 9.89 KB
/
scraper.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
from nfl_data import get_library_ids
from bs4 import BeautifulSoup
import pandas as pd
from tools import *
import threading
import requests
current_season = get_season()
def fetch_from_url(url):
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return None
def get_fantasy_pros_ids():
url = "https://www.fantasypros.com/nfl/adp/ppr-overall.php"
html_content = fetch_from_url(url)
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find_all('table')[0]
table_data = []
rows = table.find_all('tr')
for row in rows[1:]:
column = row.find_all(['th', 'td'])[1]
position = row.find_all(['th', 'td'])[2].get_text()[0:2]
if position in ["RB","WR", "TE"]:
hyperlinks = column.find_all('a')
link_name = hyperlinks[0]['href'][13:-4]
for item in hyperlinks:
try:
name = item['fp-player-name']
id = item['class'][-1].split("-")[-1]
table_data.append(
[
name,
link_name,
int(id)
]
)
break
except:
pass
fantasy_pros_df = pd.DataFrame(table_data, columns=["name", "link", 'fantasypros_id'])
nfl_data_df = get_library_ids()
merged_df = pd.merge(fantasy_pros_df, nfl_data_df, on='fantasypros_id', how='inner')
merged_df = merged_df[['name', 'link', 'position']]
return merged_df
fantasy_pros_ids = get_fantasy_pros_ids()
def get_fantasy_pros_names():
return fantasy_pros_ids.name.unique().tolist()
def format_name(player):
return str(fantasy_pros_ids[fantasy_pros_ids["name"]==player]['link'].iloc[0])
def get_position(player):
return str(fantasy_pros_ids[fantasy_pros_ids["name"]==player]['position'].iloc[0])
def scrape_table(url, index=0):
html_content = fetch_from_url(url)
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find_all('table')[index]
table_data = []
rows = table.find_all('tr')
for row in rows:
row_data = []
columns = row.find_all(['th', 'td'])
for column in columns:
value = column.get_text(strip=True)
try:
row_data.append(float(value))
except:
row_data.append(value)
table_data.append(row_data)
return table_data
def get_position_projections(position, week):
position = position.lower()
url = f"https://www.fantasypros.com/nfl/projections/{position}.php?week={week}&scoring=PPR"
data = scrape_table(url)
receiver = data[0][1]=="RECEIVING"
if position not in ["k", "dst"]:
data = data[1:]
if position in ["rb", "wr"]:
if receiver:
data[0][2] = "REC YDS"
data[0][3] = "REC TDS"
data[0][5] = "RUSH YDS"
data[0][6] = "RUSH TDS"
else:
data[0][2] = "RUSH YDS"
data[0][3] = "RUSH TDS"
data[0][5] = "REC YDS"
data[0][6] = "REC TDS"
df = pd.DataFrame(data[1:], columns=data[0])
df["Player"] = df["Player"].apply(fantasy_pros_format)
return df
def get_player_projection(player):
player = format_name(player)
url=f"https://www.fantasypros.com/nfl/projections/{player}.php?scoring=PPR"
data = scrape_table(url, index=0)
return pd.DataFrame(data[1:], columns=data[0])
def get_player_weekly_stats(player, year=current_season):
player = format_name(player)
url = f"https://www.fantasypros.com/nfl/games/{player}.php?scoring=PPR&season={year}"
data = scrape_table(url)
receiver = data[0][1]=="Receiving"
data = data[1:-1]
for index in range(1, len(data)):
data[index][0] = int(data[index][0].replace("Week ",""))
data[0][0] = "Week"
if receiver:
data[0][5] = "rec yards"
data[0][8] = "rec tds"
data[0][10] = "rush yards"
data[0][13] = "rush tds"
else:
data[0][4] = "rush yards"
data[0][7] = "rush tds"
data[0][10] = "rec yards"
data[0][13] = "rec tds"
data = [item for item in data if item[1] != 'BYE Week']
data = [item for item in data if item[3] != '-']
df = pd.DataFrame(data[1:], columns=data[0])
return df.sort_values(by="Week")
def get_points_against(position, year=current_season):
position = (["QB", "RB", "WR", "TE"].index(position.upper())+1)*10
url = f"https://www.fftoday.com/stats/fantasystats.php?Season={year}&GameWeek=Season&PosID={position}&Side=Allowed&LeagueID=107644"
data = scrape_table(url, 7)[1:]
data[0] = [
'Team',
'G',
'Att',
'Rushing Yards',
'Rushing TDs',
'Rec',
'Receiving Yards',
'Receiving TDs',
'FPts',
'FPts/G'
]
for index in range(1, len(data)):
value = data[index][0]
start = value.index(".")+1
end = value.index(" ")
data[index][0] = data[index][0][start:end]
for column_index in range(1, len(data[index])):
value = data[index][column_index]
if type(value) is str and "," in value:
data[index][column_index]=float(value.replace(",",""))
df = pd.DataFrame(data[1:], columns=data[0])
df = df.drop("G", axis=1)
return df
def get_schedule(player):
player = format_name(player)
url = f"https://www.fantasypros.com/nfl/schedule/{player}.php"
next_opponent = scrape_table(url, 0)[1][1]
data = scrape_table(url, 1)[1:]
for index in range(1, len(data)):
data[index][0] = int(data[index][0].replace("Week ",""))
df = pd.DataFrame(data[1:], columns=data[0])
df = df.drop(columns=['Game Time', 'Matchup Rating'])
last_week_played = float(df[df['Opp'] == next_opponent]["Week"].iloc[0])-1
df = df[df['Opp'] != 'BYE']
df["Played"] = df["Week"].apply(lambda x:False if x>last_week_played else True)
return df
def get_z_score_projections(player, points_against_dfs, year=current_season):
teams_database=get_abbreviations()
position = get_position(player)
points_against_df = points_against_dfs[position]
player_df = get_player_weekly_stats(player, year)
teams_mean = points_against_df["FPts"].mean()
teams_st_dev = points_against_df["FPts"].std()
projections = []
completed_games = []
if len(player_df)<4:
last_year_df = get_player_weekly_stats(player, year-1)
last_year_df["Week"]=0
player_df = pd.concat([player_df, last_year_df], ignore_index=True)
if len(player_df)<4:
return None, None
player_mean = player_df["Points"].mean()
player_st_dev = player_df["Points"].std()
for _, row in get_schedule(player).iterrows():
if not row["Played"]:
opponent = row["Opp"].replace("vs. ","").replace("@ ","")
team = teams_database[opponent]
team_average = points_against_df[points_against_df['Team'] == team]['FPts']
z_score = (float(team_average.iloc[0]) - teams_mean) / teams_st_dev
projection = player_mean + z_score*player_st_dev
projections.append(projection)
elif row["Week"] in player_df['Week'].values:
fantasy_points = player_df[player_df['Week'] == row["Week"]]['Points']
completed_games.append(float(fantasy_points.iloc[0]))
return projections, completed_games, position
def process_player(player, points_against_dfs, data, lock):
try:
projections, _, position = get_z_score_projections(player, points_against_dfs)
if projections is not None and len(projections) > 0:
with lock:
data.append(
[
player,
position.upper(),
round(sum(projections)/len(projections), 1)
]
)
except:
pass
def get_mass_z_predictions(players=None, year=current_season):
if players is None:
players = []
for position in ["rb","wr", "te"]:
url = f"https://www.fantasypros.com/nfl/stats/{position}.php?scoring=PPR"
players += [fantasy_pros_format(player_row[1]) for player_row in scrape_table(url)[2:]][0:50]
points_against_dfs = {
"WR":get_points_against("WR", year),
"RB":get_points_against("RB", year),
"TE":get_points_against("TE", year)
}
data = []
threads = []
data_lock = threading.Lock()
for player in players:
arguments = (player, points_against_dfs, data, data_lock)
thread = threading.Thread(target=process_player, args=arguments)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
return data
def get_team_stats_df(year=current_season):
stat_type = ["passing","rushing","receiving","scoring"]
df = None
for stat in stat_type:
url = f"https://www.nfl.com/stats/team-stats/offense/{stat}/{year}/reg/all"
data = scrape_table(url)
columns = data[0]
if columns[1]=="Att":
columns[1]= stat.capitalize()+" Att"
new_df = pd.DataFrame(data[1:], columns=columns).iloc[:, :3]
if df is None:
df = new_df
else:
df = df.merge(new_df, on='Team', how='outer')
df['Team'] = df['Team'].apply(format_team)
df.rename(columns={'Yds': 'Rec Yds'}, inplace=True)
return df
def team_stats(year=current_season):
df = get_team_stats_df(year)
output_data = {}
for _, row in df.iterrows():
output_data[row['Team']] = {
"Attempts": [
row['Passing Att'],
row['Rushing Att']
],
"TDs": [
row["Rec TD"],
row["Rsh TD"]
],
"Yards": [
row["Rec Yds"],
row["Rush Yds"]
]
}
return output_data