def paged_github_json_request(url, headers=None):
response = requests.get(url, headers=headers)
assert response.ok, response.content
results = response.json()
if 'Link' in response.headers:
links = response.headers['Link']
# There are likely better ways to parse/extract the link information
# but here we just find the last page number mentioned in the header
# 'Link' section and then loop over all pages to get the comments
last_match = list(re.finditer('page=[0-9]+', links))[-1]
last_page = int(links[last_match.start():last_match.end()].split('=')[1])
# If there are other pages, just loop over them and get all the
# comments
if last_page > 1:
for page in range(2, last_page + 1):
response = requests.get(url + '?page={0}'.format(page), headers=headers)
assert response.ok, response.content
results += response.json()
return results
评论列表
文章目录