-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_starters_reserves.py
57 lines (50 loc) · 1.76 KB
/
grab_starters_reserves.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
from bs4 import BeautifulSoup
from urllib.request import urlopen
from Player import Player
def get_url():
url = input("Enter url of boxscore to scrape: ")
team = input("What team are you scouting? ")
return url, team
def scrape_boxscore(url, team):
players = []
home_vis = ""
page = urlopen(url)
soup = BeautifulSoup(page, "html.parser")
box = None
# Find the correct table - search for full-game boxscore of team being scouted, assign it to box
for table in soup.findAll('table'):
try:
if table.caption.h2.span.text == team:
if 'home' in table.parent.parent.parent.get('class', ''):
home_vis = "home"
else:
home_vis = "visitor"
box = table
break
except:
continue
starters = box.findAll('tbody')[0]
for row in starters.findAll('th', attrs={'scope': 'row'}):
num = row.span.text.split(" -")[0]
try:
name = row.a.text
oua_id = row.a['href']
except AttributeError:
name = row.find('span', attrs={'class': 'player-name'}).text
oua_id = None
player = Player(name, num, oua_id, "starter")
players.append(player)
reserves = box.findAll('tbody')[1]
for row in reserves.findAll('th', attrs={'scope': 'row'}):
num = row.span.text.split(" -")[0]
if num == "TM":
continue
try:
name = row.a.text
oua_id = row.a['href']
except AttributeError:
name = row.find('span', attrs={'class': 'player-name'}).text
oua_id = None
player = Player(name, num, oua_id, "reserve")
players.append(player)
return players, home_vis