-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
114 lines (107 loc) · 3.72 KB
/
Copy pathexample.js
File metadata and controls
114 lines (107 loc) · 3.72 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
114
/**
* Amazon Scraper — Scrapeless Scraping API (Node.js example)
*
* Docs: https://apidocs.scrapeless.com/doc-857373
* Token: https://app.scrapeless.com/passport/login?redirect=/quick-start
*
* The Amazon actor supports four scrape types selected via `input.type`:
* product | seller | keywords | rufus
*
* Run (Node.js 18+, uses the built-in fetch):
* export SCRAPELESS_API_TOKEN="your_api_token"
* node example.js # defaults to the "product" type
* node example.js keywords # or pass a type: product | seller | keywords | rufus
*/
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 scrape type.
const SAMPLE_INPUTS = {
product: {
type: "product",
url: "https://www.amazon.com/dp/B0BQXHK363",
zip_code: "",
},
seller: {
type: "seller",
url: "https://www.amazon.com/sp?seller=A2XZ7JICGUQ1CX",
zip_code: "",
},
keywords: {
type: "keywords",
keywords: "Iphone+14+Pro+512GB",
page: "1",
domain: "com",
zip_code: "",
},
rufus: {
type: "rufus",
keywords: "macbook",
domain: "www.amazon.es",
page: "1",
},
};
async function scrape(scrapeType) {
const input = SAMPLE_INPUTS[scrapeType];
if (!input) {
throw new Error(`Unknown type '${scrapeType}'. 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.amazon", input }),
});
// The Amazon actor distinguishes scenarios by HTTP status code.
switch (response.status) {
case 200: {
// Synchronous success: the body is the scraped data (shape depends on type).
const data = await response.json();
console.log(`[200] Success — '${scrapeType}' data received.`);
summarize(scrapeType, data);
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}`);
}
}
}
function summarize(scrapeType, data) {
if (scrapeType === "product") {
console.log(` ASIN: ${data.asin}`);
console.log(` Brand: ${data.brand}`);
console.log(` Price: ${data.final_price} (${data.availability})`);
} else if (scrapeType === "keywords") {
const organic = (data.result && data.result.organic) || [];
console.log(` keyword: ${data.keyword} page: ${data.page} — ${organic.length} organic results`);
for (const item of organic.slice(0, 3)) {
console.log(` - ${item.title} :: ${item.price}`);
}
} else {
console.log(` ${scrapeType} keys: ${Object.keys(data).slice(0, 10).join(", ")}`);
}
console.log("\n Raw response (truncated to 1500 chars):");
console.log(" " + JSON.stringify(data).slice(0, 1500));
}
const type = process.argv[2] || "product";
scrape(type).catch((err) => {
console.error(err.message);
process.exit(1);
});