Skip to content

Commit b92d138

Browse files
committed
Add pagination example
1 parent 8982c80 commit b92d138

11 files changed

+77
-4
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ To run [examples](./examples/) on your local source files, follow these steps.
111111
"serpapi": "../../../npm"
112112
},
113113
"scripts": {
114-
"start": "node example.js"
114+
"start": "node basic_example.js"
115115
}
116116
}
117117
```

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in
108108
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
109109
```
110110

111+
## Pagination
112+
113+
There's no built-in pagination support in this library, but you can always do it
114+
manually. You can find pagination examples here:
115+
116+
- [Pagination example (Node.js >= 7)](examples/node/js_node_7_up/pagination_example.js)
117+
- [Pagination example (Node.js >= 14)](examples/node/js_node_14_up/pagination_example.js)
118+
- [Pagination example (Deno)](examples/deno/pagination_example.ts)
119+
111120
## Functions
112121

113122
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

examples/deno/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
2. Run the example
1111

1212
```
13-
deno run example.ts
13+
deno run basic_example.ts
14+
deno run pagination_example.ts
1415
```
1516

1617
## Notes
File renamed without changes.

examples/deno/pagination_example.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { loadSync } from "https://deno.land/[email protected]/dotenv/mod.ts";
2+
import { config, getJson } from "../../mod.ts";
3+
4+
const { API_KEY: apiKey } = loadSync();
5+
config.api_key = apiKey;
6+
7+
// Get the first page
8+
const page = await getJson({ engine: "google", q: "Coffee" });
9+
// Parse SerpApi search URL to the next page
10+
const nextUrl = new URL(page.serpapi_pagination.next);
11+
// Extract the request parameters
12+
const nextParams = Object.fromEntries(nextUrl.searchParams);
13+
// Get the next page
14+
const nextPage = await getJson(nextParams);
15+
console.log(nextPage);

examples/node/js_node_14_up/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"serpapi": "*"
66
},
77
"scripts": {
8-
"start": "node example.js"
8+
"start": "node basic_example.js"
99
}
1010
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Example works for Node.js 14 and newer.
3+
* - Uses ESM imports which is supported from Node.js 13.2.0.
4+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility
5+
* - Uses top-level await which is supported from Node.js 14.8.0.
6+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility
7+
*/
8+
9+
import * as Dotenv from "dotenv";
10+
import { config, getJson } from "serpapi";
11+
12+
Dotenv.config();
13+
config.api_key = process.env.API_KEY;
14+
15+
// Get the first page
16+
const page = await getJson({ engine: "google", q: "Coffee" });
17+
// Parse SerpApi search URL to the next page
18+
const nextUrl = new URL(page.serpapi_pagination.next);
19+
// Extract the request parameters
20+
const nextParams = Object.fromEntries(nextUrl.searchParams);
21+
// Get the next page
22+
const nextPage = await getJson(nextParams);
23+
console.log(nextPage);

examples/node/js_node_7_up/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"serpapi": "*"
55
},
66
"scripts": {
7-
"start": "node example.js"
7+
"start": "node basic_example.js"
88
}
99
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Example works for Node.js 7 and newer.
3+
*/
4+
5+
const Dotenv = require("dotenv");
6+
const { config, getJson } = require("serpapi");
7+
const url = require("url");
8+
const qs = require("querystring");
9+
10+
Dotenv.config();
11+
config.api_key = process.env.API_KEY;
12+
13+
const run = async () => {
14+
// Get the first page
15+
const page = await getJson({ engine: "google", q: "Coffee" });
16+
// Parse SerpApi search URL to the next page
17+
const nextUrl = url.parse(page.serpapi_pagination.next);
18+
// Extract the request parameters
19+
const nextParams = qs.parse(nextUrl.query);
20+
// Get the next page
21+
const nextPage = await getJson(nextParams);
22+
console.log(nextPage);
23+
};
24+
25+
run();

0 commit comments

Comments
 (0)