Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion basketball_reference_web_scraper/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,18 @@ class Division(Enum):


class TeamTotal:
def __init__(self, team_abbreviation, totals):
def __init__(self, team_abbreviation, totals, advanced_totals):
self.team_abbreviation = team_abbreviation
self.totals = totals
self.advanced_totals = advanced_totals

@property
def offensive_rating(self):
return self.advanced_totals.offensive_rating

@property
def defensive_rating(self):
return self.advanced_totals.defensive_rating

@property
def minutes_played(self):
Expand Down
31 changes: 31 additions & 0 deletions basketball_reference_web_scraper/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ def basic_statistics_tables(self):
if table.has_basic_statistics is True
]

@property
def advanced_statistics_tables(self):
return [
table
for table in self.statistics_tables
if table.has_advanced_statistics is True
]


class StatisticsTable:
def __init__(self, html):
Expand All @@ -610,6 +618,11 @@ def __init__(self, html):
def has_basic_statistics(self):
return 'game-basic' in self.html.attrib["id"]


@property
def has_advanced_statistics(self):
return 'game-advanced' in self.html.attrib["id"]

@property
def team_abbreviation(self):
# Example id value is box-BOS-game-basic or box-BOS-game-advanced
Expand All @@ -625,6 +638,24 @@ def team_totals(self):

return None

@property
def advanced_team_totals(self):
# Team totals are stored as table footers
return AdvancedTeamTotalRow(self.html.xpath('tfoot/tr/td'))


class AdvancedTeamTotalRow:
def __init__(self, html):
self.html = html

@property
def offensive_rating(self):
return self.html[13].text_content()

@property
def defensive_rating(self):
return self.html[14].text_content()


class DailyLeadersPage:
def __init__(self, html):
Expand Down
12 changes: 8 additions & 4 deletions basketball_reference_web_scraper/http_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,14 @@ def team_box_score(self, game_url_path):
response.raise_for_status()

page = BoxScoresPage(html.fromstring(response.content))
combined_team_totals = [
TeamTotal(team_abbreviation=table.team_abbreviation, totals=table.team_totals)
for table in page.basic_statistics_tables
]
combined_team_totals = []
for i in range(len(page.basic_statistics_tables)):
table = page.basic_statistics_tables[i]
adv_table = page.advanced_statistics_tables[i]
combined_team_totals.append(
TeamTotal(team_abbreviation=table.team_abbreviation,
totals=table.team_totals,
advanced_totals=adv_table.advanced_team_totals))

return self.parser.parse_team_totals(
first_team_totals=combined_team_totals[0],
Expand Down
2 changes: 2 additions & 0 deletions basketball_reference_web_scraper/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ def parse_totals(self, team_totals, opposing_team_totals):
"turnovers": str_to_int(team_totals.turnovers),
"personal_fouls": str_to_int(team_totals.personal_fouls),
"points": str_to_int(team_totals.points),
"offensive_rating": str_to_float(team_totals.offensive_rating),
"defensive_rating": str_to_float(team_totals.defensive_rating),
}


Expand Down