-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxscore.py
142 lines (114 loc) · 2.82 KB
/
boxscore.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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
ESPN_BASE = 'http://www.espn.com'
SCOREBOARD_ENDPOINT = '/nba/scoreboard'
BOXSCORE_ENDPOINT = '/nba/boxscore?gameId={}'
GAME_ID = '401070880'
DATE_EXTENSION = '/_/date/{}'
LOGO_URL = 'http://a.espncdn.com/i/teamlogos/nba/500/{}.png'
BOXSCORE_HEADER = [
'MIN',
'FG',
'3PT',
'FT',
'OREB',
'DREB',
'REB',
'AST',
'STL',
'BLK',
'TO',
"PF",
'+/-',
'PTS'
]
BOXSCORE_ORDER = [0, 13, 7, 6, 1, 2, 3, 8, 9, 10, 11, 12, 4, 5]
GAME_STATES = {
'pregame': 0,
'live': 1,
'final': 2
}
def get_browser():
options = Options()
options.headless = True
return webdriver.Chrome(options=options)
def get_soup(html):
return BeautifulSoup(html, 'lxml')
def get_text(tag):
return tag.get_text().strip()
def dump(data, filename):
import json
with open(filename, 'w') as file:
file.write(json.dumps(data, indent=4))
# boxscore: {
# home: {
# starters: [
# {
# name:
# id:
# position
# stats: []
# }
# ],
# bench: {
# },
# totals: []
# },
# away: {
# }
# }
def parse_player(player_soup):
cols = player_soup.find_all('td')
name = cols[0]
stats_soup = cols[1:]
if len(stats_soup) == 1:
stats = get_text(stats_soup[0])
else:
stats = []
for stat in stats_soup:
stats.append(get_text(stat))
player = {
'name': get_text(name.find('span')),
'url': name.find('a')['href'],
'position': get_text(name.find('span', class_='position')),
'stats': [stats[i] for i in BOXSCORE_ORDER]
}
return player
def parse_team_total(total_soup):
cols = total_soup.find_all('td')
name = cols[0]
stats_soup = cols[1:]
stats = []
for stat in stats_soup:
stats.append(get_text(stat))
return stats
def parse_team_boxscore(box_soup):
table = box_soup.find('table', class_='mod-data')
starters_soup = table.find('tbody')
starters = [parse_player(player_soup) for player_soup in starters_soup.find_all('tr')]
bench_soup = table.find('tbody', class_='bench')
bench = [parse_player(player_soup) for player_soup in bench_soup.find_all('tr')[:-2]]
return {
'starters': starters,
'bench': bench,
'total': parse_team_total(bench_soup.find('tr', class_='totals'))
}
def get_boxscore(soup):
container = soup.find('div', id='gamepackage-boxscore-module')
if container is None:
return {}
home = parse_team_boxscore(container.find('div', class_='gamepackage-home-wrap'))
away = parse_team_boxscore(container.find('div', class_='gamepackage-away-wrap'))
return {
'home': home,
'away': away,
'header': [BOXSCORE_HEADER[i] for i in BOXSCORE_ORDER]
}
if __name__ == '__main__':
# browser = get_browser()
# browser.get(ESPN_BASE + BOXSCORE_ENDPOINT.format(GAME_ID))
with open('index.html', 'r') as file:
soup = get_soup(file.read())
boxscore = get_boxscore(soup)
dump(boxscore, 'boxscore.json')