用户帐户的 Github API 调用

sno*_*all 5 python api github github-api

嗨,我正在尝试从 Github API 获取用户的数据、他们编程的语言、他们的存储库以及他们所连接的关注者/关注者以及他们的数量。

我已经通读了文档,但没有找到任何特定于我需要的查询的内容。

目前,我已经使用这个查询来调用 https://api.github.com/search/users?q=location:uk&sort=stars&order=desc&page=1&per_page=100

但是,这会返回与我要实现的目标无关的帐户名称、网址和其他内容。我正在 Jupyter 笔记本上使用 json 和 python 请求分析这些数据。

任何人都可以分享他们的意见,谢谢。

Ber*_*tel 3

您可以使用GraphQL Api v4向用户请求您想要的特定信息。在下面的查询中,您可以搜索用户并location:uk提取他们的登录名、姓名、关注者、关注者数量、存储库、存储库数量、语言等...

{
  search(query: "location:uk", type: USER, first: 100) {
    userCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      ... on User {
        login
        name
        location
        repositories(first: 10) {
          totalCount
          nodes {
            languages(first: 2) {
              nodes {
                name
              }
            }
            name
          }
        }
        followers(first: 10) {
          totalCount
          nodes {
            login
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在资源管理器中尝试一下

对于分页,用于first: 100请求前 100 个项目,用于after: <cursor_value>请求下一页,光标值是上一页的最后一个光标,例如pageInfo.endCursor上一个查询中的值。

在 Python 中,这将是:

import json
import requests

access_token = "YOUR_ACCESS_TOKEN"

query = """
{
    search(query: "location:uk", type: USER, first: 100) {
        userCount
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          ... on User {
            login
            name
            location
            repositories(first: 10) {
              totalCount
              nodes {
                languages(first: 2) {
                  nodes {
                    name
                  }
                }
                name
              }
            }
            followers(first: 10) {
              totalCount
              nodes {
                login
              }
            }
          }
        }
    }
}"""

data = {'query': query.replace('\n', ' ')}
headers = {'Authorization': 'token ' + access_token, 'Content-Type': 'application/json'}
r = requests.post('https://api.github.com/graphql', headers=headers, json=data)
print(json.loads(r.text))
Run Code Online (Sandbox Code Playgroud)