-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpagination_example.js
More file actions
24 lines (21 loc) · 932 Bytes
/
pagination_example.js
File metadata and controls
24 lines (21 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Example works for Node.js 14 and newer.
* - Uses ESM imports which is supported from Node.js 13.2.0.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility
* - Uses top-level await which is supported from Node.js 14.8.0.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility
*/
import * as Dotenv from "dotenv";
import process from "process";
import { config, getJson } from "serpapi";
Dotenv.config();
config.api_key = process.env.API_KEY;
// Get the first page
const page = await getJson({ engine: "google", q: "Coffee" });
// Parse SerpApi search URL to the next page
const nextUrl = new URL(page.serpapi_pagination.next);
// Extract the request parameters
const nextParams = Object.fromEntries(nextUrl.searchParams);
// Get the next page
const nextPage = await getJson(nextParams);
console.log(nextPage);