-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.py
188 lines (156 loc) · 6.52 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
import requests
from bs4 import BeautifulSoup, Comment
import re
import string
import numpy as np
import pandas as pd
import time
'''
Scraping for the statistics of the final NCAA college seasons of Drafted/Undrafted NBA players between 2005 and 2019.
These years were chosen as there more advanced statistics were recorded from 2005 onwards.
Draft records (Labels) were scraped from https://basketball.realgm.com/
Final season statistics were scraped from https://www.sports-reference.com/
'''
def getColNames(url):
'''
Given a basketball.realgm.com url (to scrape the statistics headers), returns the column names of our data
'''
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
perGame = [x.text.strip() for x in soup.find(id='players_per_game').find('thead').find_all('th')][1:]
perGame.pop(-2)
for i, comment in enumerate(soup.find_all(text=lambda text: isinstance(text, Comment))):
if comment.find('id="players_advanced"') > 0:
table = BeautifulSoup(comment, 'html.parser').find("table")
advanced = [x.text.strip() for x in table.find('thead').find_all('th')][6:25]
advanced.pop(14)
output = ['ID', 'Name', 'Age', 'Position', 'Height', 'Weight', 'Season']
output.extend(perGame)
output.extend(advanced)
output.append('DraftCategory')
output.append('DraftPick')
return output
def scrapeRow(url):
'''
Given a basketball.realgm.com url, return the statistics and various player details
'''
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
ID = url.split('/')[-1].replace('.html', '')
name = soup.find('h1').text.strip()
position = soup.find('p').text.strip().split('\n')[2].strip()
height, weight = re.findall(r'\d+', soup.find('p').find('p').contents[-1].strip())
season = soup.find(id='players_per_game').find('tbody').find_all('tr')[-1].find('th').text.strip()
season = season[0:2] + season[-2:]
row = soup.find(id='players_per_game').find('tbody').find_all('tr')[-1]
perGame = [x.text.strip() for x in row.find_all('td')]
perGame.pop(-2)
for i, comment in enumerate(soup.find_all(text=lambda text: isinstance(text, Comment))):
if comment.find('id="players_advanced"') > 0:
table = BeautifulSoup(comment, 'html.parser').find("table")
advanced = list()
row = table.find('tbody').find_all('tr')[-1]
advanced = [x.text.strip() for x in row.find_all('td')][5:24]
if len(advanced) == 18:
advanced = [''] + advanced
advanced.pop(14)
return [ID, name, position, height, weight, season] + perGame + advanced
def getDraftRecords(draftYear):
'''
Given a year (between 2005 and 2019 in this case), return a list of players who
declared their draft eligiblity, along with their age draft category and draft pick.
'''
url = f'https://basketball.realgm.com/nba/draft/past_drafts/{draftYear}'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
rows = []
for player in soup.find_all('tr')[1:]:
text = player.text.split('\n')
if len(text) == 14:
pick, name, age = text[1], text[2], text[8]
if name == 'Player':
continue
else:
pick = int(pick)
age = int(age)
if pick <= 15:
rows.append([name, age, 'Top15', pick])
elif pick <= 30:
rows.append([name, age, 'Top30', pick])
elif pick <= 45:
rows.append([name, age, 'Top45', pick])
else:
rows.append([name, age, 'Top60', pick])
else:
name, age = text[1], text[5]
if name == 'Player':
continue
else:
age = int(age)
rows.append([name, age, 'Undrafted', 80])
return rows
def scrapAndSave(year):
'''
Given a year, scrape for all the players drafted/undrafted.
Due to inconsistent formatting on sports-reference.com and names that
are recorded differently between the sites, the errors will have to be further
cleaned afterwards.
'''
data = []
nonStandardName = []
duplicates = []
notFound = []
errors = []
records = getDraftRecords(year)
for player in records:
name, age, labelCat, labelPick = player[0], player[1], player[2], player[3]
split_name = name.replace('.', '').lower().split(' ')
if len(split_name) != 2:
nonStandardName.append([year] + player)
continue
else:
first, last = split_name[0], split_name[1]
counter = 1
while True:
url = f'https://www.sports-reference.com/cbb/players/{first}-{last}-{counter}.html'
if requests.get(url).status_code == 404:
counter -= 1
break
counter += 1
if counter > 1:
sameFinalSeason = []
for i in range(1, counter + 1):
url = f'https://www.sports-reference.com/cbb/players/{first}-{last}-{i}.html'
try:
season = scrapeRow(url)[5]
except:
continue
if int(season) == year:
sameFinalSeason.append(url)
if len(sameFinalSeason) == 1:
url = sameFinalSeason[0]
row = scrapeRow(url)
data.append([row[0], name, age] + row[2:] + [labelCat, labelPick])
else:
duplicates.append([year] + player)
elif counter == 0:
notFound.append([year] + player)
else:
url = f'https://www.sports-reference.com/cbb/players/{first}-{last}-1.html'
try:
row = scrapeRow(url)
data.append([row[0], name, age] + row[2:] + [labelCat, labelPick])
except:
errors.append([year] + player)
return data, nonStandardName, duplicates, notFound, errors
def main():
'''
Scrape for the players between 2005-2019 and save each as npy files for further cleaning
'''
for year in range(2005, 2020):
start = time.time()
out = scrapAndSave(year)
print(f'Year {year} done! It took {(time.time() - start) / 60} minutes.')
np.save(f'data/{year}.npy', out)
if __name__ == "__main__":
main()