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

前端 未结 6 2052
借酒劲吻你
借酒劲吻你 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条回答
  •  故里飘歌
    2021-02-05 18:42

    From my understanding, link will be None if only a single page of data is returned, otherwise link will be present even when going beyond the last page. In this case link will contain previous and first links.

    Here is some sample python which aims to simply return the link for the next page, and returns None if there is no next page. So could incorporate in a loop.

    link = r.headers['link']
    if link is None:
        return None
    
    # Should be a comma separated string of links
    links = link.split(',')
    
    for link in links:
        # If there is a 'next' link return the URL between the angle brackets, or None
        if 'rel="next"' in link:
            return link[link.find("<")+1:link.find(">")]
    return None
    

提交回复
热议问题