Github API调用用户帐户 [英] Github API call for user accounts

查看:161
本文介绍了Github API调用用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Github API中获取用户,他们使用的编程语言,他们的存储库以及与之相关的关注者/关注者以及他们的编号的数据.

Hi I'm trying to get data from the Github API for users, the languages they programme in, their repos and the followers/follows they are connected with and their number.

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

I've read through the documentation but haven't found anything specific to the query that I need.

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

Currently, I've used this query to call https://api.github.com/search/users?q=location:uk&sort=stars&order=desc&page=1&per_page=100

但是,这将返回帐户名,URL和其他与我要实现的目标无关的内容.我正在使用Jupyter笔记本上的json和python请求分析此数据.

However, this returns the account name, url and other things that aren't relevant to what I'm trying to achieve. I'm analysing this data with json and python requests on Jupyter notebook.

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

Can anyone share their input, thanks.

推荐答案

您可以使用 GraphQL Api v4 能够向用户请求您想要的特定信息.在以下查询中,您使用location:uk&提取他们的登录名,名称,他们的关注者,关注者计数,存储库,存储库计数,语言等...

You can use GraphQL Api v4 to be able to request the specific information you want from the users. In the following query, you search user with location:uk & extract their login, name, their followers, follower count, repositories, repository count, languages etc...

{
  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的值.

For the pagination, use first: 100 to request the first 100 items & use after: <cursor_value> to request the next page, the cursor value is the last cursor of the previous page eg the value of pageInfo.endCursor in the previous query.

在Python中,这将是:

In Python, this would be :

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

这篇关于Github API调用用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆