【问题标题】:Using Python and GitHub's API : TypeError: string indices must be integers使用 Python 和 GitHub 的 API:TypeError: string indices must be integers
【发布时间】:2020-10-18 08:20:00
【问题描述】:

我正在尝试使用 GitHub 的 JSON API 来提取包含受让人的所有拉取请求的列表(这样我就可以向他们发送消息以解决他们的拉取请求)。

我觉得我很接近以下代码,但也许我没有正确解析 JSON?任何帮助将不胜感激。

import requests

url = "https://api.github.com/repos/<repo>/<repoguide>/pulls?" 

payload = {}
headers = {
  'Authorization': 'Bearer <mastertoken>',
  'Accept': 'application/vnd.github.v3+json',
  'page': 'page=5&per_page=100'
}

response = requests.request("GET", url, headers=headers, data = payload)
response = requests.get(url).json()
for obj in response:
   
    assignees = obj['assignees']
    for assignee in assignees :
      name = assignee['login']
      url = obj['html_url']
      print('/md Hello {0}, there is a GitHub issue that needs your attention. For more information see [here]({1}).'.format(name,url))

错误:

    assignees = obj['assignees']
TypeError: string indices must be integers

我不明白这个错误是什么意思或如何解决它。它是拉字符串还是字典?抱歉,这是我第一次写代码。

【问题讨论】:

    标签: python json api parsing github


    【解决方案1】:

    作为explained here,如果你得到“string indices must be integersobj['assignees'],表示obj是字符串,不是字典。

    由于similar error was tricky to be reproduced,我将打印response 的值以进行调试,以检查其性质。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 2017-10-31
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2020-01-20
      • 2020-09-04
      • 1970-01-01
      相关资源
      最近更新 更多