-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path02_serp_api.js
More file actions
68 lines (58 loc) · 1.94 KB
/
Copy path02_serp_api.js
File metadata and controls
68 lines (58 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Bright Data SERP API — Get search engine results without being blocked.
*
* Returns raw HTML or parsed search results from Google, Bing, and other engines.
*
* Setup:
* 1. Get your API key from https://brightdata.com/cp/setting/users
* 2. Find your SERP zone name from the control panel
* 3. Set environment variables:
* export BRIGHTDATA_API_KEY="your_api_key"
* export BRIGHTDATA_SERP_ZONE="your_serp_zone_name"
*
* Usage:
* node 02_serp_api.js
*/
const API_KEY = process.env.BRIGHTDATA_API_KEY;
const SERP_ZONE = process.env.BRIGHTDATA_SERP_ZONE || "serp_api1";
async function searchGoogle(query, numResults = 10) {
const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(query)}&num=${numResults}`;
const response = await fetch("https://api.brightdata.com/request", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
zone: SERP_ZONE,
url: searchUrl,
format: "raw",
}),
});
if (!response.ok) {
throw new Error(`SERP API error: ${response.status} ${await response.text()}`);
}
return response.text();
}
async function searchGoogleParsed(query, numResults = 10) {
const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(query)}&num=${numResults}`;
const response = await fetch("https://api.brightdata.com/request", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
zone: SERP_ZONE,
url: searchUrl,
format: "json",
}),
});
if (!response.ok) {
throw new Error(`SERP API error: ${response.status} ${await response.text()}`);
}
return response.json();
}
// --- Run ---
const results = await searchGoogleParsed("best javascript web scraping libraries");
console.log(JSON.stringify(results, null, 2).slice(0, 2000));