serpapi / google-search-results-nodejs

SerpApi client library for Node.js. Previously: Google Search Results Node.js.
https://serpapi.com
MIT License
78 stars 22 forks source link

Support pagination via async iterator #9

Open ilyazub opened 3 years ago

ilyazub commented 3 years ago

Python wrapper for SerpApi has search.pagination(): serpapi/google-search-results-python#19.

pages = search.pagination(0, 20)

urls = []

for result in pages:
    urls.append(result['serpapi_pagination']['next'])

In JavaScript, such a paginator can be done using the async generator function.

const paginator = search.pagination({ start: 0, end: 20 });

const organicResults = [];

for await (const page of paginator) {
  // page contains a single paginated output.
  organicResults.push(...page.organic_results);
}

Reference implementation from aws-sdk-js-v3.


Related to #6.

/cc @hartator @jvmvik