-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo2-scrape.mjs
More file actions
33 lines (27 loc) · 986 Bytes
/
demo2-scrape.mjs
File metadata and controls
33 lines (27 loc) · 986 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
25
26
27
28
29
30
31
32
33
const BROWSERLESS_TOKEN = "YOUR_API_TOKEN_HERE";
const response = await fetch(
`https://production-sfo.browserless.io/scrape?token=${BROWSERLESS_TOKEN}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
url: "https://www.scrapingcourse.com/ecommerce/",
elements: [
{ selector: ".product-name" },
{ selector: ".price" },
{ selector: ".product-image" },
],
}),
}
);
const data = await response.json();
// Each element returns an array of results matching the selector
const [names, prices, images] = data.data;
console.log(`Found ${names.results.length} products:\n`);
for (let i = 0; i < Math.min(5, names.results.length); i++) {
console.log(`${i + 1}. ${names.results[i].text}`);
console.log(` Price: ${prices.results[i].text}`);
console.log(` Image: ${images.results[i].text}`);
console.log();
}
console.log(`... and ${names.results.length - 5} more products`);