From 59123c039c9b5ba2341270e7389d4e7a343ed2e5 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 5 Feb 2025 21:56:30 -0500 Subject: [PATCH 1/2] make combined totals available as a parameter --- basketball_reference_web_scraper/html.py | 11 +++++------ basketball_reference_web_scraper/http_service.py | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/basketball_reference_web_scraper/html.py b/basketball_reference_web_scraper/html.py index 94871e65..3ae6c9f7 100644 --- a/basketball_reference_web_scraper/html.py +++ b/basketball_reference_web_scraper/html.py @@ -265,10 +265,10 @@ def get_rows(self, include_combined_totals=False): player_advanced_season_totals_rows = [] for row_html in self.html.xpath(self.rows_query): row = PlayerAdvancedSeasonTotalsRow(html=row_html) - if (include_combined_totals is True and row.is_combined_totals is True) or row.is_combined_totals is False: + if (include_combined_totals and row.is_combined_totals) or not row.is_combined_totals: # Basketball Reference includes a "total" row for players that got traded # which is essentially a sum of all player team rows - # I want to avoid including those, so I check the "team" field value for "TOT" + # I want to avoid including those by default, so I check the "team" field value for "TOT" player_advanced_season_totals_rows.append(row) return player_advanced_season_totals_rows @@ -291,15 +291,14 @@ def rows_query(self): ] """ - @property - def rows(self): + def get_rows(self, include_combined_totals=False): player_season_totals_rows = [] for row_html in self.html.xpath(self.rows_query): row = PlayerSeasonTotalsRow(html=row_html) # Basketball Reference includes a "total" row for players that got traded # which is essentially a sum of all player team rows - # I want to avoid including those, so I check the "team" field value for "TOT" - if not row.is_combined_totals: + # I want to avoid including those by default, so I check the "team" field value for "TOT" + if not row.is_combined_totals or (row.is_combined_totals and include_combined_totals): player_season_totals_rows.append(row) return player_season_totals_rows diff --git a/basketball_reference_web_scraper/http_service.py b/basketball_reference_web_scraper/http_service.py index 466b566a..c86fb820 100644 --- a/basketball_reference_web_scraper/http_service.py +++ b/basketball_reference_web_scraper/http_service.py @@ -120,7 +120,7 @@ def players_advanced_season_totals(self, season_end_year, include_combined_value table = PlayerAdvancedSeasonTotalsTable(html=html.fromstring(response.content)) return self.parser.parse_player_advanced_season_totals_parser(totals=table.get_rows(include_combined_values)) - def players_season_totals(self, season_end_year): + def players_season_totals(self, season_end_year, include_combined_values=False): url = '{BASE_URL}/leagues/NBA_{season_end_year}_totals.html'.format( BASE_URL=HTTPService.BASE_URL, season_end_year=season_end_year, @@ -131,7 +131,7 @@ def players_season_totals(self, season_end_year): response.raise_for_status() table = PlayerSeasonTotalTable(html=html.fromstring(response.content)) - return self.parser.parse_player_season_totals(totals=table.rows) + return self.parser.parse_player_season_totals(totals=table.get_rows(include_combined_values)) def schedule_for_month(self, url): response = requests.get(url=url) From fe992f42af264ea88c260a33c4715108cabd7819 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 5 Feb 2025 22:16:59 -0500 Subject: [PATCH 2/2] propagate param --- basketball_reference_web_scraper/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/basketball_reference_web_scraper/client.py b/basketball_reference_web_scraper/client.py index 2694c5f3..a793ea06 100644 --- a/basketball_reference_web_scraper/client.py +++ b/basketball_reference_web_scraper/client.py @@ -142,10 +142,10 @@ def season_schedule(season_end_year, output_type=None, output_file_path=None, ou def players_season_totals(season_end_year, output_type=None, output_file_path=None, output_write_option=None, - json_options=None): + json_options=None, include_combined_values=False): try: http_service = HTTPService(parser=ParserService()) - values = http_service.players_season_totals(season_end_year=season_end_year) + values = http_service.players_season_totals(season_end_year=season_end_year, include_combined_values=include_combined_values) except requests.exceptions.HTTPError as http_error: if http_error.response.status_code == requests.codes.not_found: raise InvalidSeason(season_end_year=season_end_year)