英文:
How can I get all data from a JSON api when pagination is enabled?
问题
我需要从这个 API 获取所有数据:
<https://hn.algolia.com/api/v1/search_by_date?query=nodejs>,但是我正在使用这段代码获取第一页(第1页)的数据:
urlApi = 'https://hn.algolia.com/api/v1/search_by_date?query=nodejs'
let repo = await axios.get(urlApi);
repo.data.hits.map((data) => {
console.log(data);
});
这个 JSON 包含 'nbPages': 50
,但是我需要映射所有50个 JSON 页面。
你有映射的任何想法吗?
英文:
I need to get all data from this api:
<https://hn.algolia.com/api/v1/search_by_date?query=nodejs>, but I'm getting the first page(page 1) data with this code:
urlApi = 'https://hn.algolia.com/api/v1/search_by_date?query=nodejs'
let repo = await axios.get(urlApi);
repo.data.hits.map((data) => {
console.log(data);
});
This JSON contains 'nbPages': 50
, But I need to map all 50 JSON pages.
Do you have any ideas for mapping?
答案1
得分: 4
你可以使用一个while循环,将命中结果追加到一个数组中,直到完成所有请求:
let repo = null, page = 0, results = [];
do {
repo = await axios.get(`${urlApi}&page=${page++}`);
results = results.concat(repo.data.hits);
} while(repo.data.page < repo.data.nbPages)
console.log(results);
英文:
You could use a while loop and just append the hits to an array until you've completed all requests:
let repo = null, page = 0, results = [];
do {
repo = await axios.get(`${urlApi}&page=${page++}`);
results = results.concat(repo.data.hits);
} while(repo.data.page < repo.data.nbPages)
console.log(results);
答案2
得分: 3
由于“hitsPerPage”的标准值为20,您可以通过使用以下URL将此值设置为一次获取所有命中(20个命中* 50页=1000):
https://hn.algolia.com/api/v1/search_by_date?query=nodejs&hitsPerPage=1000
英文:
Due to the fact that the standard value for "hitsPerPage" is 20, you can set this value to get all of the hits at once by using this URL instead (20 hits * 50 pages = 1000):
https://hn.algolia.com/api/v1/search_by_date?query=nodejs&hitsPerPage=1000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论