-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path01_web_scraper_api.js
More file actions
53 lines (46 loc) · 1.63 KB
/
Copy path01_web_scraper_api.js
File metadata and controls
53 lines (46 loc) · 1.63 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
/**
* Bright Data Web Scraper API — Scrape structured data from any supported website.
*
* Returns structured JSON (product info, reviews, profiles, etc.) from popular
* websites like Amazon, LinkedIn, Instagram, and more.
*
* Setup:
* 1. Get your API key from https://brightdata.com/cp/setting/users
* 2. Find your dataset_id from your Web Scraper zone in the control panel
* 3. Set environment variables:
* export BRIGHTDATA_API_KEY="your_api_key"
* export BRIGHTDATA_DATASET_ID="your_dataset_id"
*
* Usage:
* node 01_web_scraper_api.js
*/
const API_KEY = process.env.BRIGHTDATA_API_KEY;
const DATASET_ID = process.env.BRIGHTDATA_DATASET_ID || "gd_l1viktl72bvl7bjuj0";
async function scrape(urls, format = "json") {
const response = await fetch(
`https://api.brightdata.com/datasets/v3/scrape?dataset_id=${DATASET_ID}&format=${format}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(urls.map((url) => ({ url }))),
}
);
if (!response.ok) {
throw new Error(`API error: ${response.status} ${await response.text()}`);
}
const data = await response.json();
// 202 = async request (takes longer than 1 min)
if (response.status === 202) {
console.log(`Request queued. Snapshot ID: ${data.snapshot_id}`);
console.log(
`Poll with: GET https://api.brightdata.com/datasets/v3/progress/${data.snapshot_id}`
);
}
return data;
}
// --- Run ---
const result = await scrape(["https://www.amazon.com/dp/B0CRMZHDG8"]);
console.log(JSON.stringify(result, null, 2));