How to get data from all pages in Github API with Python?

前端 未结 6 2117
借酒劲吻你
借酒劲吻你 2021-02-05 17:55

I\'m trying to export a repo list and it always returns me information about the 1rst page. I could extend the number of items per page using URL+\"?per_page=100\" but it\'s not

6条回答
  •  旧时难觅i
    2021-02-05 18:37

    Extending on the answers above, here is a recursive function to deal with the GitHub pagination that will iterate through all pages, concatenating the list with each recursive call and finally returning the complete list when there are no more pages to retrieve, unless the optional failsafe returns the list when there are more than 500 items.

    import requests
    
    api_get_users = 'https://api.github.com/users'
    
    
    def call_api(apicall, **kwargs):
    
        data = kwargs.get('page', [])
    
        resp = requests.get(apicall)
        data += resp.json()
    
        # failsafe
        if len(data) > 500:
            return (data)
    
        if 'next' in resp.links.keys():
            return (call_api(resp.links['next']['url'], page=data))
    
        return (data)
    
    
    data = call_api(api_get_users)
    

提交回复
热议问题