-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path04_web_unlocker.js
More file actions
45 lines (40 loc) · 1.29 KB
/
Copy path04_web_unlocker.js
File metadata and controls
45 lines (40 loc) · 1.29 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
/**
* Bright Data Web Unlocker — Access any website without blocks.
*
* Automatically handles CAPTCHAs, browser fingerprinting, retries, and IP rotation.
* Returns the raw HTML of the target page.
*
* Setup:
* 1. Get your API key from https://brightdata.com/cp/setting/users
* 2. Find your Web Unlocker zone name from the control panel
* 3. Set environment variables:
* export BRIGHTDATA_API_KEY="your_api_key"
* export BRIGHTDATA_UNLOCKER_ZONE="your_web_unlocker_zone_name"
*
* Usage:
* node 04_web_unlocker.js
*/
const API_KEY = process.env.BRIGHTDATA_API_KEY;
const UNLOCKER_ZONE = process.env.BRIGHTDATA_UNLOCKER_ZONE || "web_unlocker1";
async function unlockUrl(url) {
const response = await fetch("https://api.brightdata.com/request", {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
zone: UNLOCKER_ZONE,
url,
format: "raw",
}),
});
if (!response.ok) {
throw new Error(`Web Unlocker error: ${response.status} ${await response.text()}`);
}
return response.text();
}
// --- Run ---
const html = await unlockUrl("https://www.example.com");
console.log(`Received ${html.length} chars of HTML`);
console.log(html.slice(0, 500));