From 20d3ec01ded8d8e00579186ee896bdf03f5ca32b Mon Sep 17 00:00:00 2001 From: Robert Clark Date: Thu, 12 Nov 2020 21:36:02 -0600 Subject: [PATCH] Fix issue pulling football league positions Occasionally, a team's league position will not be posted on their team page, throwing an error while attempting to parse that information. Defaulting the value to None in this scenario will skip the error while allowing expected functionality to continue. Signed-Off-By: Robert Clark --- sportsreference/fb/team.py | 7 +++++-- tests/unit/test_fb_team.py | 9 ++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/sportsreference/fb/team.py b/sportsreference/fb/team.py index 28f255c2..f9921f1a 100644 --- a/sportsreference/fb/team.py +++ b/sportsreference/fb/team.py @@ -165,12 +165,15 @@ def _records(self, record_line): records = record_line.lower().replace('record: ', '') records_split = records.split(',') if len(records_split) != 3: - return None, None, None + return None, None, None, None record, points, position = records_split points = re.sub(' point.*', '', points).strip() position = re.sub(r'\(.*\)', '', position).strip() league = re.sub('.* in ', '', position).title() - position = re.findall(r'\d+', position)[0] + try: + position = re.findall(r'\d+', position)[0] + except IndexError: + position = None return record, points, position, league def _goals(self, goals_line): diff --git a/tests/unit/test_fb_team.py b/tests/unit/test_fb_team.py index d40d4fde..ecbdb247 100644 --- a/tests/unit/test_fb_team.py +++ b/tests/unit/test_fb_team.py @@ -52,7 +52,14 @@ def test_records_missing_result(self): output = self.team._records(html) - assert output == (None, None, None) + assert output == (None, None, None, None) + + def test_records_missing_position(self): + html = 'Record: 5-0-0, 15 points (3.0 per game), Premier League' + + output = self.team._records(html) + + assert output == ('5-0-0', '15', None, 'Premier League') def test_goals_missing(self): html = 'Goals: 20 (2.5 per game), Goals Against: 11 (1.38 per game)'