Github API调用用户账户

5

你好,我正在尝试从Github API获取用户的数据,包括他们所编程的语言、仓库和他们连接的关注者/被关注者数目。

我已经阅读了文档,但没有找到我需要的特定查询。

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

然而,这个查询返回的是账户名、URL和其他与我的目标无关的信息。我正在使用Jupyter笔记本上的json和python请求来分析这些数据。

有人能够分享他们的建议吗?谢谢。


你有没有查看过:https://developer.github.com/v3/users/followers/? - Projjol
是的,我有,但似乎只有在搜索特定用户及其关注者时才会出现,但我需要一个包含该特定位置内所有用户及其关注者的数据集。@Projjol - snow_fall
1个回答

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
          }
        }
      }
    }
  }
}

在资源浏览器中尝试

对于分页,使用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))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接