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

前端 未结 6 2041
借酒劲吻你
借酒劲吻你 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:34

    From github docs:

    Response:

    Status: 200 OK
    Link: ; rel="next",
          ; rel="last"
    X-RateLimit-Limit: 5000
    X-RateLimit-Remaining: 4999
    

    You get the links to the next and the last page of that organization. Just check the headers.

    On Python Requests, you can access your headers with:

    response.headers
    

    It is a dictionary containing the response headers. If link is present, then there are more pages and it will contain related information. It is recommended to traverse using those links instead of building your own.

    You can try something like this:

    import requests
    url = 'https://api.github.com/orgs/xxxxxxx/repos?page{0}&per_page=100'
    response = requests.get(url)
    link = response.headers.get('link', None)
    if link is not None:
        print link
    

    If link is not None it will be a string containing the relevant links for your resource.

提交回复
热议问题