python爬取json問題

sichon
python爬取json問題出現TypeError: string indices must be integers
讀取不到,請問應該怎麼辦


import requests as req
from openpyxl import Workbook

header ={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
}

wb = Workbook()
ws = wb.active

title = ['標題']
ws.append(title)

for index in range(10):
    url = 'https://www.tdm.com.mo/api/v1.0/news/600774?id=600774'
    url = url + str(index)
    print(url)
    r = req.get(url, headers=header)
    print(r)

    root_json = r.json()

    for data in root_json['data']:
        course = []
        course.append(data['title'])

    ws.append(course)

wb.save('data.xlsx')





回到頂部