-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbaseballReferenceScrape.py
More file actions
379 lines (355 loc) · 14.1 KB
/
baseballReferenceScrape.py
File metadata and controls
379 lines (355 loc) · 14.1 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
## Ben Kite
import pandas, numpy
import requests, bs4
import re, os
## This is the best place to get started.
## This function simply takes a url and provides the ids
## from the html tables that the code provided here can access.
## Using findTables is great for determining options for the
## pullTable function for the tableID argument.
def findTables(url):
res = requests.get(url)
## The next two lines get around the issue with comments breaking the parsing.
comm = re.compile("<!--|-->")
soup = bs4.BeautifulSoup(comm.sub("", res.text), 'lxml')
divs = soup.findAll('div', id = "content")
divs = divs[0].findAll("div", id=re.compile("^all"))
ids = []
for div in divs:
searchme = str(div.findAll("table"))
x = searchme[searchme.find("id=") + 3: searchme.find(">")]
x = x.replace("\"", "")
if len(x) > 0:
ids.append(x)
return(ids)
## For example:
## findTables("http://www.baseball-reference.com/teams/KCR/2016.shtml")
## Pulls a single table from a url provided by the user.
## The desired table should be specified by tableID.
## This function is used in all functions that do more complicated pulls.
def pullTable(url, tableID):
res = requests.get(url)
## Work around comments
comm = re.compile("<!--|-->")
soup = bs4.BeautifulSoup(comm.sub("", res.text), 'lxml')
tables = soup.findAll('table', id = tableID)
data_rows = tables[0].findAll('tr')
data_header = tables[0].findAll('thead')
data_header = data_header[0].findAll("tr")
data_header = data_header[0].findAll("th")
game_data = [[td.getText() for td in data_rows[i].findAll(['th','td'])]
for i in range(len(data_rows))
]
data = pandas.DataFrame(game_data)
header = []
for i in range(len(data.columns)):
header.append(data_header[i].getText())
data.columns = header
data = data.loc[data[header[0]] != header[0]]
data = data.reset_index(drop = True)
return(data)
## For example:
## url = "http://www.baseball-reference.com/teams/KCR/2016.shtml"
## pullTable(url, "team_batting")
## Pulls game level data for team and year provided.
## The team provided must be a three-character abbreviation:
## 'ATL', 'ARI', 'BAL', 'BOS', 'CHC', 'CHW', 'CIN', 'CLE', 'COL', 'DET',
## 'KCR', 'HOU', 'LAA', 'LAD', 'MIA', 'MIL', 'MIN', 'NYM', 'NYY', 'OAK',
## 'PHI', 'PIT', 'SDP', 'SEA', 'SFG', 'STL', 'TBR', 'TEX', 'TOR', 'WSN'
def pullGameData (team, year):
url = "http://www.baseball-reference.com/teams/" + team + "/" + str(year) + "-schedule-scores.shtml"
## Let's funnel this work into the pullTable function
dat = pullTable(url, "team_schedule")
dates = dat["Date"]
ndates = []
for d in dates:
month = d.split(" ")[1]
day = d.split(" ")[2]
day = day.zfill(2)
mapping = {"Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
"Sep": "09", "Oct": "10", "Nov":"11"}
m = mapping[month]
ndates.append(str(year) + m + day)
uni, counts = numpy.unique(ndates, return_counts = True)
ndates = []
for t in range(len(counts)):
ux = uni[t]
cx = counts[t]
if cx == 1:
ndates.append(ux + "0")
else:
for i in range(int(cx)):
ii = i + 1
ndates.append(ux + str(ii))
dat["Date"] = ndates
dat.rename(columns = {dat.columns[4] : "Location"}, inplace = True)
homegame = []
for g in dat["Location"]:
homegame.append(g == "")
dat["HomeGame"] = homegame
return(dat)
## Pulls data summarizing the season performance of all players on the
## team provided for the given year.
## The table type argument must be one of five possibilities:
## "team_batting"
## "team_pitching"
## "standard_fielding"
## "players_value_batting"
## "players_value_pitching"
def pullPlayerData (team, year, tabletype):
url = "http://www.baseball-reference.com/teams/" + team + "/" + str(year) + ".shtml"
data = pullTable(url, tabletype)
data = data[data.Name.notnull()]
data = data.reset_index(drop = True)
names = data.columns
for c in range(0, len(names)):
replacement = []
if type (data.loc[0][c]) == str:
k = names[c]
for i in range(0, len(data[k])):
p = data.loc[i][c]
xx = re.sub("[#@*&^%$!]", "", p)
xx = xx.replace("\xa0", "_")
xx = xx.replace(" ", "_")
replacement.append(xx)
data[k] = replacement
data["Team"] = team
data["Year"] = year
return(data)
## This is used later to append integers to games on the same date to
## separate them.
def Quantify (x):
out = []
for i in x:
if len(i) < 1:
out.append(None)
else:
out.append(float(i))
return(out)
## Pulls box score data from a game provided in the gameInfo input
## This is meant to be run by the pullBoxScores function below.
def gameFinder (gameInfo):
teamNames = {"KCR":"KCA",
"CHW":"CHA",
"CHC":"CHN",
"LAD":"LAN",
"NYM":"NYN",
"NYY":"NYA",
"SDP":"SDN",
"SFG":"SFN",
"STL":"SLN",
"TBR":"TBA",
"WSN":"WAS",
"LAA":"ANA"}
battingNames = {"ATL":"AtlantaBravesbatting",
"ARI":"ArizonaDiamondbacksbatting",
"BAL":"BaltimoreOriolesbatting",
"BOS":"BostonRedSoxbatting",
"CHC":"ChicagoCubsbatting",
"CHW":"ChicagoWhiteSoxbatting",
"CIN":"CincinnatiRedsbatting",
"CLE":"ClevelandIndiansbatting",
"COL":"ColoradoRockiesbatting",
"DET":"DetroitTigersbatting",
"KCR":"KansasCityRoyalsbatting",
"HOU":"HoustonAstrosbatting",
"LAA":"AnaheimAngelsbatting",
"LAD":"LosAngelesDodgersbatting",
"MIA":"MiamiMarlinsbatting",
"MIL":"MilwaukeeBrewersbatting",
"MIN":"MinnesotaTwinsbatting",
"NYM":"NewYorkMetsbatting",
"NYY":"NewYorkYankeesbatting",
"OAK":"OaklandAthleticsbatting",
"PHI":"PhiladelphiaPhilliesbatting",
"PIT":"PittsburghPiratesbatting",
"SDP":"SanDiegoPadresbatting",
"SEA":"SeattleMarinersbatting",
"SFG":"SanFranciscoGiantsbatting",
"STL":"StLouisCardinalsbatting",
"TBR":"TampaBayRaysbatting",
"TEX":"TexasRangersbatting",
"TOR":"TorontoBlueJaysbatting",
"WSN":"WashingtonNationalsbatting"}
date = gameInfo["Date"]
home = gameInfo["HomeGame"]
if home == False:
opp = gameInfo["Opp"]
if opp in teamNames:
opp = teamNames[opp]
url = "http://www.baseball-reference.com/boxes/" + opp + "/" + opp + str(date) + ".shtml"
else:
team = gameInfo["Tm"]
if team in teamNames:
team = teamNames[team]
url = "http://www.baseball-reference.com/boxes/" + team + "/" + team + str(date) + ".shtml"
battingInfo = battingNames[gameInfo["Tm"]]
data = pullTable(url, battingInfo)
names = []
for i in data["Batting"]:
if len(i) > 0:
xx = (i.split(" ")[0] + "_" + i.split(" ")[1])
xx = xx.replace("\xa0", "")
names.append(xx)
else:
names.append("NA")
data["Name"] = names
data["Date"] = date
data["HomeGame"] = home
data = data[data.Name != "NA"]
for d in data:
if d not in ["Batting", "Name", "Details", "Date", "HomeGame"]:
tmp = Quantify(data[d])
data[d] = tmp
data = data[data["AB"] > 0]
return(data)
## Pulls all of the boxscores for a team in a given year.
## The directory argument is used to specify where to save the .csv
## If overwrite is True, an existing file with the same name will be overwritten.
def pullBoxscores (team, year, directory, overwrite = True):
if not os.path.exists(directory):
os.makedirs(directory)
if overwrite == False:
if os.path.exists(directory + team + ".csv"):
return("This already exists!")
dat = pullGameData(team, year)
DatDict = dict()
for r in range(len(dat)):
inputs = dat.loc[r]
try:
DatDict[r] = gameFinder(inputs)
except IndexError:
pass
playerGameData = pandas.concat(DatDict)
playerGameData.reset_index(inplace = True)
playerGameData = playerGameData.rename(columns = {"level_0": "Game", "level_1": "BatPos"})
playerGameData.to_csv(directory + team + "_" + str(year) + ".csv")
## This is an internal function to pullPlaybyPlay
def PlayByPlay (gameInfo):
teamNames = {"KCR":"KCA",
"CHW":"CHA",
"CHC":"CHN",
"LAD":"LAN",
"NYM":"NYN",
"NYY":"NYA",
"SDP":"SDN",
"SFG":"SFN",
"STL":"SLN",
"TBR":"TBA",
"WSN":"WAS",
"LAA":"ANA"}
oteam = gameInfo["Tm"]
date = gameInfo["Date"]
home = gameInfo["HomeGame"]
if home == 0:
team = gameInfo["Opp"]
opp = gameInfo["Tm"]
if opp in teamNames:
opp = teamNames[opp]
else:
team = gameInfo["Tm"]
opp = gameInfo["Opp"]
if team in teamNames:
team = teamNames[team]
url = "http://www.baseball-reference.com/boxes/" + team + "/" + team + str(date) + ".shtml"
dat = pullTable(url, "play_by_play")
dat = dat.loc[dat["Batter"].notnull()]
dat = dat.loc[dat["Play Description"].notnull()]
dat["Date"] = date
dat["Hteam"] = team
dat["Ateam"] = opp
pteam = []
pteams = numpy.unique(dat["@Bat"])
for d in dat["@Bat"]:
if d == pteams[0]:
pteam.append(pteams[1])
else:
pteam.append(pteams[0])
dat["Pteam"] = pteam
if gameInfo["R"] > gameInfo["RA"]:
winner = oteam
else:
winner = gameInfo["Opp"]
dat["Winner"] = winner
return(dat)
## Pulls all of the play by play tables for a team for a given year.
## Output is the name of the .csv file you want to save. I force a
## file to be saved here because the function takes a while to run.
def pullPlaybyPlay (team, year, output, check = False):
dat = pullGameData(team, year)
dat = dat[dat.Time == dat.Time] ## Only pull games that have ended
if check:
olddat = pandas.read_csv(output)
dates = numpy.unique(olddat.Date)
mostrecent = numpy.max(dates)
dat.Date = dat.Date.astype("int")
dat = dat.loc[dat.Date > mostrecent]
dat.reset_index(inplace = True)
dat = dat.loc[dat.Time == dat.Time]
DatDict = dict()
for r in range(len(dat)):
inputs = dat.loc[r]
try:
DatDict[r] = PlayByPlay(inputs)
except IndexError:
pass
if len(DatDict) == 0:
return("No new games to be added!")
bdat = pandas.concat(DatDict)
bdat["Hteam"] = team
names = []
for i in bdat["Batter"]:
if len(i) > 0:
xx = i
xx = xx.replace("\xa0", "")
names.append(xx)
else:
names.append("NA")
bdat["BatterName"] = names
## These rules attempt to sort out different play outcomes by
## searching the text in the "Play Description" variable.
bdat["out"] = (bdat["Play Description"].str.contains("out")) | (bdat["Play Description"].str.contains("Play")) | (bdat["Play Description"].str.contains("Flyball")) | (bdat["Play Description"].str.contains("Popfly"))
bdat["hbp"] = bdat["Play Description"].str.startswith("Hit")
bdat["walk"] = (bdat["Play Description"].str.contains("Walk"))
bdat["stolenB"] = bdat["Play Description"].str.contains("Steal")
bdat["wild"] = bdat["Play Description"].str.startswith("Wild") | bdat["Play Description"].str.contains("Passed")
bdat["error"] = bdat["Play Description"].str.contains("Reached on")
bdat["pick"] = bdat["Play Description"].str.contains("Picked")
bdat["balk"] = bdat["Play Description"].str.contains("Balk")
bdat["interference"] = bdat["Play Description"].str.contains("Reached on Interference")
bdat["sacrifice"] = bdat["Play Description"].str.contains("Sacrifice")
bdat["ab"] = (bdat["walk"] == False) & (bdat["sacrifice"] == False) & (bdat["interference"] == False) & (bdat["stolenB"] == False) & (bdat["wild"] == False) & (bdat["hbp"] == False) & (bdat["pick"] == False) & (bdat["balk"] == False)
bdat["hit"] = (bdat["walk"] == False) & (bdat["out"] == False) & (bdat["stolenB"] == False) & (bdat["error"] == False) & (bdat["ab"] == True)
if check:
if len(olddat) > 0:
bdat = olddat.append(bdat)
bdat.reset_index(inplace = True, drop = True)
bdat.to_csv(output)
return(bdat)
## This pulls information about which hand a pitcher throws with. I
## made this solely to allow pitcher handedness to be used as a
## variable in models.
def pullPitcherData (team, year):
url = "http://www.baseball-reference.com/teams/" + team + "/" + str(year) + ".shtml"
data = pullTable(url, "team_pitching")
data = data[data.Name.notnull()]
data = data[data.Rk.notnull()]
data = data[data.G != "162"]
data = data.reset_index(drop = True)
data["Team"] = team
data["Year"] = year
data["LeftHanded"] = data["Name"].str.contains("\\*")
names = data.columns
for c in range(0, len(names)):
replacement = []
if type (data.loc[0][c]) == str:
k = names[c]
for i in range(0, len(data[k])):
p = data.loc[i][c]
xx = re.sub("[#@&*^%$!]", "", p)
xx = xx.replace("\xa0", "_")
xx = xx.replace(" ", "_")
replacement.append(xx)
data[k] = replacement
data = data[["Name", "LeftHanded", "Team", "Year"]]
return(data)