【发布时间】:2015-01-14 20:01:07
【问题描述】:
我正在使用 requests 包来访问 API (greenhouse.io)。 API 是分页的,所以我需要遍历页面来获取我想要的所有数据。使用类似的东西:
results = []
for i in range(1,326+1):
response = requests.get(url,
auth=(username, password),
params={'page':i,'per_page':100})
if response.status_code == 200:
results += response.json()
通过点击 headers 属性我知道有 326 个页面:
In [8]:
response.headers['link']
Out[8]:
'<https://harvest.greenhouse.io/v1/applications?page=3&per_page=100>; rel="next",<https://harvest.greenhouse.io/v1/applications?page=1&per_page=100>; rel="prev",<https://harvest.greenhouse.io/v1/applications?page=326&per_page=100>; rel="last"'
有没有办法自动提取这个号码?使用请求包?还是我需要使用正则表达式之类的?
或者,我应该以某种方式使用 while 循环来获取所有这些数据吗?什么是最好的方法?有什么想法吗?
【问题讨论】:
-
我所知道的所有 API 系统都将在其 JSON 响应中放入下一页的句柄(当您点击最后一页时将是
null)或至少是数字页数。您确定此信息不在您从系统获得的 JSON 响应中吗?
标签: python regex api pagination