diff --git a/pybaseball/team_batting.py b/pybaseball/team_batting.py index 8d8504dd..3c7ebcb3 100644 --- a/pybaseball/team_batting.py +++ b/pybaseball/team_batting.py @@ -40,10 +40,14 @@ def team_batting_bref(team: str, start_season: int, end_season: Optional[int]=No response = session.get(stats_url) soup = BeautifulSoup(response.content, 'html.parser') - table = soup.find_all('table', {'class': 'sortable stats_table'})[0] + table = soup.find('table', {'id': 'players_standard_batting'}) + if table is None: + raise ValueError( + "Could not find batting data for {} {}. The page structure may have changed.".format(team, season) + ) if headings is None: - headings = [row.text.strip() for row in table.find_all('th')[1:28]] + headings = [th.text.strip() for th in table.find('thead').find_all('th')[1:]] rows = table.find_all('tr') for row in rows: diff --git a/pybaseball/team_pitching.py b/pybaseball/team_pitching.py index 0facc4f3..d0893538 100644 --- a/pybaseball/team_pitching.py +++ b/pybaseball/team_pitching.py @@ -42,10 +42,14 @@ def team_pitching_bref(team: str, start_season: int, end_season: Optional[int]=N response = session.get(stats_url) soup = BeautifulSoup(response.content, 'html.parser') - table = soup.find_all('table', {'id': 'team_pitching'})[0] + table = soup.find('table', {'id': 'players_standard_pitching'}) + if table is None: + raise ValueError( + "Could not find pitching data for {} {}. The page structure may have changed.".format(team, season) + ) if headings is None: - headings = [row.text.strip() for row in table.find_all('th')[1:34]] + headings = [th.text.strip() for th in table.find('thead').find_all('th')[1:]] rows = table.find_all('tr') for row in rows: diff --git a/tests/pybaseball/data/team_batting_bref.html b/tests/pybaseball/data/team_batting_bref.html new file mode 100644 index 00000000..c0e44b04 --- /dev/null +++ b/tests/pybaseball/data/team_batting_bref.html @@ -0,0 +1,11 @@ +
+| Rk | Tm | BatAge | G | PA | R | H | HR | RBI |
|---|---|---|---|---|---|---|---|---|
| 1 | New York Yankees | 28.5 | 162 | 6300 | 900 | 1400 | 250 | 860 |
| 2 | Boston Red Sox | 27.9 | 162 | 6250 | 850 | 1450 | 220 | 820 |
| Rk | Tm | PAge | G | IP | H | ER | ERA | SO | BB |
|---|---|---|---|---|---|---|---|---|---|
| 1 | New York Yankees | 29.1 | 162 | 1450.0 | 1300 | 600 | 3.72 | 1500 | 500 |
| 2 | Boston Red Sox | 28.4 | 162 | 1440.0 | 1350 | 650 | 4.06 | 1400 | 520 |
no table here
") + + with pytest.raises(ValueError): + team_batting_bref('NYY', 2019) diff --git a/tests/pybaseball/test_team_pitching.py b/tests/pybaseball/test_team_pitching.py index 5a554fc9..61888971 100644 --- a/tests/pybaseball/test_team_pitching.py +++ b/tests/pybaseball/test_team_pitching.py @@ -45,3 +45,36 @@ def test_team_pitching_relievers(response_get_monkeypatch: Callable, sample_html team_pitching_relievers_result = team_pitching_relievers(season).reset_index(drop=True) pd.testing.assert_frame_equal(team_pitching_relievers_result, sample_processed_result, check_dtype=False) + + +@pytest.fixture(name="sample_bref_html") +def _sample_bref_html(get_data_file_contents: Callable[[str], str]) -> str: + return get_data_file_contents('team_pitching_bref.html') + + +def test_team_pitching_bref(bref_get_monkeypatch: Callable, sample_bref_html: str) -> None: + # Regression test for #461: Baseball Reference changed the pitching table to + # id='players_standard_pitching' with a . team_pitching_bref must parse + # the new structure instead of the removed 'team_pitching' id. + from pybaseball.team_pitching import team_pitching_bref + + bref_get_monkeypatch(sample_bref_html) + + result = team_pitching_bref('NYY', 2019) + + assert result is not None + assert not result.empty + assert 'Tm' in result.columns + assert 'Year' in result.columns + assert (result['Year'] == 2019).all() + + +def test_team_pitching_bref_missing_table_raises(bref_get_monkeypatch: Callable) -> None: + # Regression test for #461: a page without the expected table should raise a + # clear ValueError instead of an opaque IndexError. + from pybaseball.team_pitching import team_pitching_bref + + bref_get_monkeypatch("no table here
") + + with pytest.raises(ValueError): + team_pitching_bref('NYY', 2019)