diff --git a/basketball_reference_web_scraper/data.py b/basketball_reference_web_scraper/data.py index 1b4390f4..69b299de 100644 --- a/basketball_reference_web_scraper/data.py +++ b/basketball_reference_web_scraper/data.py @@ -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): diff --git a/basketball_reference_web_scraper/html.py b/basketball_reference_web_scraper/html.py index ec109a28..d5f71bad 100644 --- a/basketball_reference_web_scraper/html.py +++ b/basketball_reference_web_scraper/html.py @@ -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): @@ -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 @@ -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): diff --git a/basketball_reference_web_scraper/http_service.py b/basketball_reference_web_scraper/http_service.py index 932dc8c4..aa3b9921 100644 --- a/basketball_reference_web_scraper/http_service.py +++ b/basketball_reference_web_scraper/http_service.py @@ -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], diff --git a/basketball_reference_web_scraper/parsers.py b/basketball_reference_web_scraper/parsers.py index 7a9cb173..62b2f257 100644 --- a/basketball_reference_web_scraper/parsers.py +++ b/basketball_reference_web_scraper/parsers.py @@ -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), }