如何在启用分页时从JSON API获取所有数据?

huangapple go评论78阅读模式
英文:

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 包含 &#39;nbPages&#39;: 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 = &#39;https://hn.algolia.com/api/v1/search_by_date?query=nodejs&#39;

let repo = await axios.get(urlApi);
repo.data.hits.map((data) =&gt; {
    console.log(data);
 });

This JSON contains &#39;nbPages&#39;: 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}&amp;page=${page++}`);
    results = results.concat(repo.data.hits);
} while(repo.data.page &lt; 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&amp;hitsPerPage=1000

huangapple
  • 本文由 发表于 2020年1月4日 01:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定