解析github api ..获取字符串索引必须是整数错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析github api ..获取字符串索引必须是整数错误相关的知识,希望对你有一定的参考价值。
我需要循环提交并从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'
需要得到提交者,日期和他们的消息的人。
答案
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'
以上是关于解析github api ..获取字符串索引必须是整数错误的主要内容,如果未能解决你的问题,请参考以下文章
类型错误:发出 REST API 请求时,字符串索引必须是整数