如何获取从Github API提取的请求数据的结果页数? [英] How to get number of result pages for the data fetched from Github API for a request?

查看:272
本文介绍了如何获取从Github API提取的请求数据的结果页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用CURL,而仅使用jQuery,AJAX和JS从Github API获取信息.我正在使用这样的网址来获取有关问题的信息- https://api.github.com/repos/jquery/jquery/issues

I am not using CURL and only using jQuery, AJAX and JS for getting information from Github API. I am using a URL like this to get information about issues- https://api.github.com/repos/jquery/jquery/issues

但是,由于Github API使用了分页功能,因此结果会出现在多个页面中.使用CURL时,我们会了解标头信息,其中还会显示结果页数,但我没有使用CURL,而是使用jQuery和AJAX直接从上述API URL请求数据,因此我无法获得上述标头信息网址.我想使用上面的jquery/jquery存储库和其他一些存储库的URL来计算打开和关闭的问题以及打开和关闭的PR的数量,但是由于某些存储库存在很多问题,因此我得到了多个页面的结果

But the result comes in multiple pages since Github API uses pagination feature. When using CURL we get to know about the header information in which the number of result pages are also shown but I am not using CURL and directly requesting data from above API url using jQuery and AJAX so I am unable to get the header information for above URL. I want to count the number of open and closed issues and open and closed PRs using the above URL for jquery/jquery repository and some other repositoris as well but since there is a lot of issues for some repositories, I am getting result in multiple pages.

我知道可以通过URL传递的"page"和"per_page" GET参数,以获取该结果页面并像这样在每页显示一些结果(例如-100)- https://api.github.com/repos/jquery /jquery/issues?page = 5& per_page = 100

I know about the "page" and "per_page" GET parameter that can be passed through the URL to get that result page and to display a number of results( e.g - 100) per page like this- https://api.github.com/repos/jquery/jquery/issues?page=5&per_page=100

我不想手动检查结果页数.我希望我的脚本自动获取结果页面信息的数量,以便我可以创建一个循环并遍历所有页面以获取有关所有问题的信息.

I don't want to check the number of result pages manually. I want my script to get the number of result pages information automatically so that I can create a loop and iterate through all the pages to get information about all the issues.

例如如果我知道结果页的数量是8,那么我可以创建一个像这样的循环来从所有结果页中获取有关所有问题的信息-

e.g. if I get to know that the number of result pages are 8 then I can create a loop like this to get information about all the issues from all the result pages-

var number_of_pages=8;
var issues_information;
for(var nof=1; nof<=number_of_result_pages;nof++){
    var URL='https://api.github.com/repos/jquery/jquery/issues?page='+nof+'&per_page=100';
    $.getJSON(URL, function(json)){
        issues_information=json;
    }
}

"issues_information"将获取从Github API获取的JSON数据.但是我无法获得特定API调用的结果页数.

Where "issues_information" will get JSON data that is fetched from Github API. But I am unable to get the count of result pages for a particular API call.

有人可以告诉我如何从Github API获取请求结果页数吗?请提供示例代码,URL格式等.

Can anybody tell me how to get number of result pages from Github API for a request? Please give an example code, URL format etc.

推荐答案

来自文档:

有关分页的信息在API的Link标头中提供 称呼.例如,让我们向搜索API发出curl请求,以 找出Mozilla项目使用短语addClass的次数:

Information about pagination is provided in the Link header of an API call. For example, let's make a curl request to the search API, to find out how many times Mozilla projects use the phrase addClass:

curl -I "https://api.github.com/search/code?q=addClass+user:mozilla" The -I

参数表示我们只关心标题,而不关心标题 实际内容.在检查结果时,您会注意到一些 链接标题中的信息如下所示:

parameter indicates that we only care about the headers, not the actual content. In examining the result, you'll notice some information in the Link header that looks like this:

Link:
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>;
rel="next",  
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>;
rel="last" 

让我们分解一下. rel ="next"表示下一页 是page = 2.这是有道理的,因为默认情况下,所有分页查询 从第1页开始.rel ="last"提供了更多信息,说明 结果的最后一页在第34页.因此,我们还有33个 我们可以使用的有关addClass的信息页面.很好!

Let's break that down. rel="next" says that the next page is page=2. This makes sense, since by default, all paginated queries start at page 1. rel="last" provides some more information, stating that the last page of results is on page 34. Thus, we have 33 more pages of information about addClass that we can consume. Nice!

因此要迭代整个页面,只需继续请求页面,直到链接标题中没有"next"为止.

So to iterate overall the pages, just keep requesting pages until there is no "next" in the link header.

以下是一些显示逻辑的python代码:

Here is some python code showing the logic:

params = {'page': 1, 'per_page':100}
another_page = True
api = GH_API_URL+'orgs/'+org['login']+'/teams'
while another_page: #the list of teams is paginated
    r = requests.get(api, params=params, auth=(username, password))
    json_response = json.loads(r.text)
    results.append(json_response)
    if 'next' in r.links: #check if there is another page of organisations
        api = r.links['next']['url']
    else:
        another_page=False

这篇关于如何获取从Github API提取的请求数据的结果页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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