-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
113 lines (107 loc) · 3.39 KB
/
Copy pathexample.js
File metadata and controls
113 lines (107 loc) · 3.39 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Google Scraper — Scrapeless Scraping API (Node.js example)
*
* Docs: https://apidocs.scrapeless.com/doc-800321
* https://apidocs.scrapeless.com/doc-1275927
* Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
*
* The Google actor (`scraper.google.search`) selects a search vertical via the
* `tbm` input field:
* web (default) | images (isch) | local (lcl) | video (vid) | shopping (shop) | news (nws)
*
* Run (Node.js 18+, uses the built-in fetch):
* export SCRAPELESS_API_TOKEN="your_api_token"
* node example.js # defaults to the "web" vertical
* node example.js images # or: web | images | local | video | shopping
*/
const API_URL = "https://api.scrapeless.com/api/v1/scraper/request";
const API_TOKEN = process.env.SCRAPELESS_API_TOKEN || "YOUR_API_TOKEN";
// Ready-to-use input payloads for each search vertical.
const SAMPLE_INPUTS = {
web: {
q: "coffee",
hl: "en",
gl: "us",
google_domain: "google.com",
},
images: {
q: "Apple Iphone16",
hl: "en",
gl: "us",
google_domain: "google.com",
tbm: "isch",
},
local: {
q: "Coffee",
hl: "en",
gl: "us",
google_domain: "google.com",
tbm: "lcl",
},
video: {
q: "Coffee",
google_domain: "google.com",
start: 0,
num: 10,
tbm: "vid",
},
shopping: {
q: "Coffee",
google_domain: "google.com",
start: 0,
num: 10,
tbm: "shop",
},
};
async function scrape(vertical) {
const input = SAMPLE_INPUTS[vertical];
if (!input) {
throw new Error(`Unknown vertical '${vertical}'. Choose one of: ${Object.keys(SAMPLE_INPUTS).join(", ")}`);
}
const response = await fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-token": API_TOKEN,
},
body: JSON.stringify({ actor: "scraper.google.search", input }),
});
// The Scraping API distinguishes scenarios by HTTP status code.
switch (response.status) {
case 200: {
// Synchronous success: the body is the scraped SERP data.
const data = await response.json();
console.log(`[200] Success — '${vertical}' results received.`);
if (data && typeof data === "object") {
console.log(` top-level keys: ${Object.keys(data).slice(0, 12).join(", ")}`);
}
console.log("\n Raw response (truncated to 1500 chars):");
console.log(" " + JSON.stringify(data).slice(0, 1500));
return data;
}
case 201: {
// Task accepted but still running. Retrieve it later by task id
// (async retrieval / webhook — see the official documentation).
const body = await response.json();
console.log(`[201] Task in progress — message: ${body.message}, taskId: ${body.taskId}`);
console.log(" Fetch the result later using the task id (see docs).");
return body;
}
case 400: {
// Scraping failed — inspect the error code and message.
const body = await response.json();
console.log(`[400] Bad request — code: ${body.code}, message: ${body.message}`);
return body;
}
default: {
const text = await response.text();
console.log(`[${response.status}] Unexpected response:\n${text}`);
throw new Error(`Unexpected status ${response.status}`);
}
}
}
const vertical = process.argv[2] || "web";
scrape(vertical).catch((err) => {
console.error(err.message);
process.exit(1);
});