-
Notifications
You must be signed in to change notification settings - Fork 2
/
web_scrape_mlb.py
358 lines (356 loc) · 13.6 KB
/
web_scrape_mlb.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
html parse code - MLB
@author: brianszekely
"""
import requests
from bs4 import BeautifulSoup
from pandas import DataFrame
from numpy import nan
from time import sleep
from os.path import join, exists
from os import getcwd
from urllib import request
from urllib.request import Request, urlopen
from pandas import read_csv
from numpy import where
from re import search
from difflib import get_close_matches
def get_data_team(team,year):
#example url: https://www.baseball-reference.com/teams/tgl.cgi?team=NYY&t=b&year=2022
#Batting URL
URL_b = f'https://www.baseball-reference.com/teams/tgl.cgi?team={team}&t=b&year={year}'
hdr = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}
req_1 = Request(URL_b,headers=hdr)
html_1 = request.urlopen(req_1)
soup_bat = BeautifulSoup(html_1, "html.parser")
sleep(3)
#Pitching
URL_p = f'https://www.baseball-reference.com/teams/tgl.cgi?team={team}&t=p&year={year}'
hdr = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}
req_1 = Request(URL_p,headers=hdr)
html_1 = request.urlopen(req_1)
soup_pitch = BeautifulSoup(html_1, "html.parser")
sleep(3)
#Game-by-Game schedule
URL_sched = f'https://www.baseball-reference.com/teams/{team}/{year}-schedule-scores.shtml'
hdr = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}
req_1 = Request(URL_sched,headers=hdr)
html_1 = request.urlopen(req_1)
soup_sched = BeautifulSoup(html_1, "html.parser")
#Batting data
game_result = []
PA = []
AB = []
R = []
H = []
second_base = []
third_base = []
HR = []
RBI = []
BB = []
IBB = []
SO = []
HBP = []
SH = []
SF = []
ROE = []
GIDP = []
SB = []
CS = []
batting_avg = []
onbase_perc = []
slugging_perc = []
onbase_plus_slugging = []
LOB = []
batters_number = []
team_homeORaway = []
table = soup_bat.find(id='all_team_batting_gamelogs')
tbody = table.find('tbody')
tr_body = tbody.find_all('tr')
for trb in tr_body:
for td in trb.find_all('td'):
if td.get('data-stat') == "game_result":
if 'W' in td.get_text():
game_result.append(1)
else:
game_result.append(0)
if td.get('data-stat') == "team_homeORaway":
#Away = 0, Home = 1
if '@' in td.get_text():
team_homeORaway.append(0)
else:
team_homeORaway.append(1)
if td.get('data-stat') == 'PA':
PA.append(td.get_text())
if td.get('data-stat') == 'AB':
AB.append(td.get_text())
if td.get('data-stat') == 'R':
R.append(td.get_text())
if td.get('data-stat') == 'H':
H.append(td.get_text())
if td.get('data-stat') == '2B':
second_base.append(td.get_text())
if td.get('data-stat') == '3B':
third_base.append(td.get_text())
if td.get('data-stat') == 'HR':
HR.append(td.get_text())
if td.get('data-stat') == 'RBI':
RBI.append(td.get_text())
if td.get('data-stat') == 'BB':
BB.append(td.get_text())
if td.get('data-stat') == 'IBB':
IBB.append(td.get_text())
if td.get('data-stat') == 'SO':
SO.append(td.get_text())
if td.get('data-stat') == 'HBP':
HBP.append(td.get_text())
if td.get('data-stat') == 'SH':
SH.append(td.get_text())
if td.get('data-stat') == 'SF':
SF.append(td.get_text())
if td.get('data-stat') == 'ROE':
ROE.append(td.get_text())
if td.get('data-stat') == 'GIDP':
GIDP.append(td.get_text())
if td.get('data-stat') == 'SB':
SB.append(td.get_text())
if td.get('data-stat') == 'CS':
CS.append(td.get_text())
if td.get('data-stat') == 'batting_avg':
batting_avg.append(td.get_text())
if td.get('data-stat') == 'onbase_perc':
onbase_perc.append(td.get_text())
if td.get('data-stat') == 'slugging_perc':
slugging_perc.append(td.get_text())
if td.get('data-stat') == 'onbase_plus_slugging':
onbase_plus_slugging.append(td.get_text())
if td.get('data-stat') == 'LOB':
LOB.append(td.get_text())
if td.get('data-stat') == 'batters_number':
batters_number.append(td.get_text())
#Pitching Data
table = soup_pitch.find(id='all_team_pitching_gamelogs')
tbody = table.find('tbody')
tr_body = tbody.find_all('tr')
IP = []
Hits_allow = []
Runs_allow = []
ER = []
UER = []
BB_allow = []
SO_pitch = []
HR_allow = []
HBP_pitch = []
earned_run_avg = []
batters_faced = []
pitches = []
strikes_total = []
inherited_runners = []
inherited_score = []
SB_allow = []
CS_pitch = []
AB_pitch = []
allow_2B = []
allow_3B = []
IBB_allow = []
SH_allow = []
SF_allow = []
ROE_allow = []
GIDP_allow = []
pitchers_number = []
for trb in tr_body:
for td in trb.find_all('td'):
if td.get('data-stat') == 'IP':
IP.append(td.get_text())
if td.get('data-stat') == 'H':
Hits_allow.append(td.get_text())
if td.get('data-stat') == 'R':
Runs_allow.append(td.get_text())
if td.get('data-stat') == 'ER':
ER.append(td.get_text())
if td.get('data-stat') == 'UER':
UER.append(td.get_text())
if td.get('data-stat') == 'BB':
BB_allow.append(td.get_text())
if td.get('data-stat') == 'SO':
SO_pitch.append(td.get_text())
if td.get('data-stat') == 'HR':
HR_allow.append(td.get_text())
if td.get('data-stat') == 'HBP':
HBP_pitch.append(td.get_text())
if td.get('data-stat') == 'earned_run_avg':
earned_run_avg.append(td.get_text())
if td.get('data-stat') == 'batters_faced':
batters_faced.append(td.get_text())
if td.get('data-stat') == 'pitches':
pitches.append(td.get_text())
if td.get('data-stat') == 'strikes_total':
strikes_total.append(td.get_text())
if td.get('data-stat') == 'inherited_runners':
inherited_runners.append(td.get_text())
if td.get('data-stat') == 'inherited_score':
inherited_score.append(td.get_text())
if td.get('data-stat') == 'SB':
SB_allow.append(td.get_text())
if td.get('data-stat') == 'CS':
CS_pitch.append(td.get_text())
if td.get('data-stat') == 'AB':
AB_pitch.append(td.get_text())
if td.get('data-stat') == '2B':
allow_2B.append(td.get_text())
if td.get('data-stat') == '3B':
allow_3B.append(td.get_text())
if td.get('data-stat') == 'IBB':
IBB_allow.append(td.get_text())
if td.get('data-stat') == 'SH':
SH_allow.append(td.get_text())
if td.get('data-stat') == 'SF':
SF_allow.append(td.get_text())
if td.get('data-stat') == 'ROE':
ROE_allow.append(td.get_text())
if td.get('data-stat') == 'GIDP':
GIDP_allow.append(td.get_text())
if td.get('data-stat') == 'pitchers_number':
pitchers_number.append(td.get_text())
#Game-by-Game
table = soup_sched.find(id='div_team_schedule')
tbody = table.find('tbody')
tr_body = tbody.find_all('tr')
rank = []
cli = []
RA = []
RS = []
win_loss_streak = []
for trb in tr_body:
for td in trb.find_all('td'):
if td.get('data-stat') == 'rank':
rank.append(td.get_text())
if td.get('data-stat') == 'cli':
cli.append(td.get_text())
if td.get('data-stat') == 'RA':
RA.append(td.get_text())
if td.get('data-stat') == 'R':
RS.append(td.get_text())
if td.get('data-stat') == 'win_loss_streak':
count = len(td.get_text())
value = -count if "-" in td.get_text() else count
win_loss_streak.append(value)
# dict_val = {"game_result":game_result,
# "PA":PA,
# "AB":AB,
# "R":R,
# "H":H,
# "second_base":second_base,
# "third_base":third_base,
# "HR":HR,
# "RBI":RBI,
# "BB":BB,
# "IBB":IBB,
# "SO":SO,
# "HBP":HBP,
# "SH":SH,
# "SF":SF,
# "ROE":ROE,
# "GIDP":GIDP,
# "SB":SB,
# "CS":CS,
# "ROE":ROE,
# "batting_avg":batting_avg,
# "onbase_perc":onbase_perc,
# "slugging_perc":slugging_perc,
# "onbase_plus_slugging":onbase_plus_slugging,
# "batters_number":batters_number,
# "LOB":LOB,
# "IP":IP,
# "Hits_allow":Hits_allow,
# "Runs_allow":Runs_allow,
# "ER":ER,
# "UER":UER,
# "BB_allow":BB_allow,
# "SO_pitch":SO_pitch,
# "HR_allow":HR_allow,
# "HBP_pitch":HBP_pitch,
# "earned_run_avg":earned_run_avg,
# "batters_faced":batters_faced,
# "pitches":pitches,
# "strikes_total":strikes_total,
# "inherited_runners":inherited_runners,
# "inherited_score":inherited_score,
# "SB_allow":SB_allow,
# "CS_pitch":CS_pitch,
# "AB_pitch":AB_pitch,
# "allow_2B":allow_2B,
# "allow_3B":allow_3B,
# "IBB_allow":IBB_allow,
# "SH_allow":SH_allow,
# "SF_allow":SF_allow,
# "ROE_allow":ROE_allow,
# "GIDP_allow":GIDP_allow,
# "pitchers_number":pitchers_number,
# "game_location":team_homeORaway,
# 'rank':rank,
# "cli":cli
# }
# for key, value in dict_val.items():
# print(key, len(value))
return DataFrame({"game_result":game_result,
"RS":RS,
"RA":RA,
"PA":PA,
"AB":AB,
"H":H,
"second_base":second_base,
"third_base":third_base,
"HR":HR,
"RBI":RBI,
"BB":BB,
"IBB":IBB,
"SO":SO,
"HBP":HBP,
"SH":SH,
"SF":SF,
"ROE":ROE,
"GIDP":GIDP,
"SB":SB,
"CS":CS,
"ROE":ROE,
"batting_avg":batting_avg,
"onbase_perc":onbase_perc,
"slugging_perc":slugging_perc,
"onbase_plus_slugging":onbase_plus_slugging,
"batters_number":batters_number,
"LOB":LOB,
"IP":IP,
"Hits_allow":Hits_allow,
"Runs_allow":Runs_allow,
"ER":ER,
"UER":UER,
"BB_allow":BB_allow,
"SO_pitch":SO_pitch,
"HR_allow":HR_allow,
"HBP_pitch":HBP_pitch,
"earned_run_avg":earned_run_avg,
"batters_faced":batters_faced,
"pitches":pitches,
"strikes_total":strikes_total,
"inherited_runners":inherited_runners,
"inherited_score":inherited_score,
"SB_allow":SB_allow,
"CS_pitch":CS_pitch,
"AB_pitch":AB_pitch,
"allow_2B":allow_2B,
"allow_3B":allow_3B,
"IBB_allow":IBB_allow,
"SH_allow":SH_allow,
"SF_allow":SF_allow,
"ROE_allow":ROE_allow,
"GIDP_allow":GIDP_allow,
"pitchers_number":pitchers_number,
"game_location":team_homeORaway,
'rank':rank,
"win_loss_streak":win_loss_streak
# "cli":cli
})
# get_data_team('NYY',2022)