serpapi / google-search-results-nodejs

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

Add an example of paginated searches #6

Open ilyazub opened 3 years ago

ilyazub commented 3 years ago

There is no documentation for paginated searches. For example, the Python wrapper has such documentation.

Pagination can be done like this:

// Try it online: https://replit.com/@serpapi/scrape-google-organic-results-urls-nodejs-serpapi

const SerpApi = require("google-search-results-nodejs");
const search = new SerpApi.GoogleSearch(process.env["API_KEY"]);

const params = {
  q: "watch movies online free",
  location: "Austin, TX",
};

function paginationCallback(result) {
  console.log(`\nCurrent page: ${result.serpapi_pagination.current}\n`);

  for (organicResult of result.organic_results) {
    console.log(organicResult.link);
  }

  const num = params.num || 10;

  search.json(
    {
      ...params,
      start: result.serpapi_pagination.current * num,
    },
    paginationCallback
  );
}

search.json(params, paginationCallback);

Sample output

Current page: 1

https://www.popcornflix.com/
https://tubitv.com/home
https://www.vudu.com/content/movies/uxpage/View-All-Free-Movies-TV/207
https://editorial.rottentomatoes.com/article/fresh-movies-you-can-watch-for-free-online-right-now/
https://www.netflix.com/
https://www.peacocktv.com/stream/movies
https://www.openculture.com/freemoviesonline
https://www.hulu.com/hub/movies
https://www.crackle.com/movies

Current page: 2

https://www.hotstar.com/us/movies
https://www.androidauthority.com/free-movies-online-streaming-1099293/
https://troypoint.com/free-online-movie-streaming-sites/
https://higherlogicdownload.s3.amazonaws.com/AMERICANBAR/UploadedImages/81986f90-2bb9-4235-8fc5-ded4d2894ca1/f9-fast-and-furious-9-niwq.pdf
https://www.komando.com/tv-streaming-reviews/best-sites-to-watch-movies-for-free/604808/
https://filmzie.com/
https://www.ctv.ca/movies
https://created.crayola.com/network/members/profile?UserKey=18eb5f43-080b-4c84-b8f1-f0870ca7dfec
https://www.fossmint.com/watch-free-movies-and-tv-shows-online/
https://vimeo.com/channels/1398936/videos/page:2

Also some search.pagination() wrapper can be added like for Python: https://github.com/serpapi/google-search-results-python/issues/19.

@jvmvik @hartator What do you think?