I am getting IndexError: list index out of range for my code below. I am trying to collect the game info for just the NFL games that are current. I have found two ways to identify the games in the JSON, but getting this error when I run the code. I'm new to python, so apologies if the answer is an obvious one. Thanks.
# US FOOTBALL EVENT INFO API LINK
link = 'http://xxxxxx.net/v1/feeds/sportsbookv2/event/group/1000093199.json?app_id=21e03bb8&app_key' \
'=c0191eaf354cc07bc3769817b8c1acd0&local=en_AU&includeparticipants=false&site=www.xxxx.com.au '
# Request data from link as 'str'
data = requests.get(link).text
# convert 'str' to Json
data = json.loads(data)
# JSON PARSE
for event_data in data['events']:
if event_data['group'] == 'NFL' and event_data['tags'][3] == 'MATCH':
competition = event_data['group']
event_id = event_data['id']
event_name = event_data['name']
event_start = event_data['start']
event_status = event_data['state']
print(competition, event_id, event_name, event_start, event_status)
this prints out the following:
NFL 1018663095 Denver Broncos - Indianapolis Colts 2022-10-07T00:15:00Z NOT_STARTED
NFL 1018663094 Buffalo Bills - Pittsburgh Steelers 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663086 Cleveland Browns - Los Angeles Chargers 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663084 Jacksonville Jaguars - Houston Texans 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663083 Minnesota Vikings - Chicago Bears 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663082 New England Patriots - Detroit Lions 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663081 New Orleans Saints - Seattle Seahawks 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018667663 New York Jets - Miami Dolphins 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663077 Tampa Bay Buccaneers - Atlanta Falcons 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663074 Washington Commanders - Tennessee Titans 2022-10-09T17:00:00Z NOT_STARTED
NFL 1018663073 Carolina Panthers - San Francisco 49ers 2022-10-09T20:05:00Z NOT_STARTED
NFL 1018663072 Arizona Cardinals - Philadelphia Eagles 2022-10-09T20:25:00Z NOT_STARTED
NFL 1018663069 Los Angeles Rams - Dallas Cowboys 2022-10-09T20:25:00Z NOT_STARTED
NFL 1018663039 Baltimore Ravens - Cincinnati Bengals 2022-10-10T00:20:00Z NOT_STARTED
NFL 1018663037 Kansas City Chiefs - Las Vegas Raiders 2022-10-11T00:15:00Z NOT_STARTED
Traceback (most recent call last):
File "C:\Users\xxxxx\OneDrive\Desktop\xxxxx-functions\testing.py", line 44, in <module>
event_info()
File "C:\Users\xxxxx\OneDrive\Desktop\xxxxx-functions\testing.py", line 23, in event_info
if event_data['group'] == 'NFL' and event_data['tags'][3] == 'MATCH':
IndexError: list index out of range
In the JSON for the NFL games there is a key called "tags" which I am using to identify which are the games (other markets available in the JSON which i dont want to collect with this function). The tags look like this
{
"englishName": "Cleveland Browns - Los Angeles Chargers",
"groupId": 1000093656,
"homeName": "Cleveland Browns",
"groupSortOrder": 2876820019684212736,
"path":
[
{
"englishName": "American Football",
"termKey": "american_football",
"name": "American Football",
"id": 1000093199
},
{
"englishName": "NFL",
"termKey": "nfl",
"name": "NFL",
"id": 1000093656
}
],
"id": 1018663086,
"state": "NOT_STARTED",
"group": "NFL",
"start": "2022-10-09T17:00:00Z",
"nonLiveBoCount": 144,
"tags":
[
"",
"OFFERED_LIVE",
"BET_BUILDER",
"MATCH"
],
"awayName": "Los Angeles Chargers",
"name": "Cleveland Browns - Los Angeles Chargers",
"sport": "AMERICAN_FOOTBALL"
}
but i think the error is coming from when the other leagues are scanned and the tags key only has
{
"englishName": "Montreal Alouettes - Ottawa RedBlacks",
"groupId": 1000093370,
"homeName": "Montreal Alouettes",
"groupSortOrder": 3874820019684212736,
"path":
[
{
"englishName": "American Football",
"termKey": "american_football",
"name": "American Football",
"id": 1000093199
},
{
"englishName": "CFL",
"termKey": "cfl",
"name": "CFL",
"id": 1000093370
}
],
"id": 1018400365,
"state": "NOT_STARTED",
"group": "CFL",
"start": "2022-10-10T17:00:00Z",
"nonLiveBoCount": 3,
"tags":
[
"OFFERED_LIVE",
"BET_BUILDER",
"MATCH"
],
"awayName": "Ottawa RedBlacks",
"name": "Montreal Alouettes - Ottawa RedBlacks",
"sport": "AMERICAN_FOOTBALL"
}
The error tells you what you need to know here, it even references the line number
The problem is with event_data['tags'][3]
You're reaching a value in your data where event_data['tags'] has less than 4 entries so index 3 is out of bounds.
Throw your code in a Try block and handle the error if it comes up
try:
if event_data['group'] == 'NFL' and event_data['tags'][3] == 'MATCH':
#your other code here
except(IndexError):
continue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.