解析github api ..获取字符串索引必须是整数错误

问题描述 投票:0回答:1

我需要循环提交并从GitHub API获取名称,日期和消息信息。

https://api.github.com/repos/droptable461/Project-Project-Management/commits

我有很多不同的东西,但我一直卡在字符串索引必须是整数错误:

def git():
#name , date , message
#https://api.github.com/repos/droptable461/Project-Project-Management/commits
#commit { author { name and date
#commit { message

    #with urlopen('https://api.github.com/repos/droptable461/Project Project-Management/commits') as response:
        #source = response.read()

    #data = json.loads(source)
    #state = []
    #for state in data['committer']:
        #state.append(state['name'])
        #print(state)

    link = 'https://api.github.com/repos/droptable461/Project-Project-Management/events'
    r = requests.get('https://api.github.com/repos/droptable461/Project-Project-Management/commits')
    #print(r)

    #one = r['commit']
    #print(one)
    for item in r.json():
        for c in item['commit']['committer']:
            print(c['name'],c['date'])

    return 'suc'

需要得到提交者,日期和他们的消息的人。

python json python-3.x parsing github-api
1个回答
0
投票

item['commit']['committer']是一个字典对象,因此行: for c in item['commit']['committer']:正在转换字典键。

由于您在字符串(字典键)上调用[],因此您收到错误。

相反,代码看起来应该更像:

def git():
    link = 'https://api.github.com/repos/droptable461/Project-Project-Management/events'
    r = requests.get('https://api.github.com/repos/droptable461/Project-Project-Management/commits')
    for item in r.json():
        for key in item['commit']['committer']:
            print(item['commit']['committer']['name'])
            print(item['commit']['committer']['date'])
            print(item['commit']['message'])
    return 'suc'
© www.soinside.com 2019 - 2024. All rights reserved.