Owen
Owen

Reputation: 21

Python / JSON - TypeError: list indices must be integers or slices, not str

I am trying to get matchID from JSON data, however, got below TypeError.Could someone please advise solution here?

URL: https://bet.hkjc.com/football/getJSON.aspx?jsontype=schedule.aspx

from selenium import webdriver
import requests
import json

match_day_url = 'https://bet.hkjc.com/football/getJSON.aspx?jsontype=schedule.aspx'
driver = webdriver.Chrome(options=options,executable_path=r"XXXXXX")
driver.get(match_day_url)
time.sleep(3)
#print(driver.page_source,'lxml')


pre = driver.find_element_by_tag_name("pre").text
data = json.loads(pre)
matchID = data['matchID']
print (matchID)

Error: TypeError: list indices must be integers or slices, not str

Thanks in advance.

Upvotes: 2

Views: 319

Answers (1)

Yash
Yash

Reputation: 1281

This is because data is a list of json. So you need:

for d in data: 
    matchID = d['matchID']
    print (matchID)

Upvotes: 1

Related Questions