【问题标题】:Python / JSON - TypeError: list indices must be integers or slices, not strPython / JSON - TypeError:列表索引必须是整数或切片,而不是 str
【发布时间】:2021-01-07 10:15:50
【问题描述】:

我正在尝试从 JSON 数据中获取 matchID,但是,它低于 TypeError。有人可以在这里建议解决方案吗?

网址: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)

错误: TypeError: 列表索引必须是整数或切片,而不是 str

提前致谢。

【问题讨论】:

    标签: python json


    【解决方案1】:

    这是因为data是一个json列表。所以你需要:

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

    【讨论】:

    • 明白了。感谢您的回答。
    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2020-11-21
    • 2017-10-08
    • 1970-01-01
    • 2015-12-09
    • 2021-11-28
    相关资源
    最近更新 更多