parse github api.. getting string indices must be integers 错误

parse github api.. getting string indices must be integers error

我需要遍历提交并从中获取名称、日期和消息信息 GitHubAPI。

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'

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

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'