【问题标题】:Github API call for user accounts用户帐户的 Github API 调用
【发布时间】:2018-08-22 20:51:50
【问题描述】:

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

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

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

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

谁能分享他们的意见,谢谢。

【问题讨论】:

  • 是的,但它似乎只在搜索特定用户及其关注者时才出现,但我需要一个包含该特定位置内的所有用户及其关注者@Projjol 的数据集

标签: python api github github-api


【解决方案1】:

您可以使用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
          }
        }
      }
    }
  }
}

Try it in the explorer

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2019-08-28
    • 2022-12-01
    • 1970-01-01
    相关资源
    最近更新 更多