-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path04_web_unlocker.py
More file actions
68 lines (56 loc) · 2.1 KB
/
Copy path04_web_unlocker.py
File metadata and controls
68 lines (56 loc) · 2.1 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
"""
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. Works with both the API and proxy interface.
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:
python 04_web_unlocker.py
"""
import os
import requests
API_KEY = os.environ["BRIGHTDATA_API_KEY"]
UNLOCKER_ZONE = os.environ.get("BRIGHTDATA_UNLOCKER_ZONE", "web_unlocker1")
def unlock_url(url: str) -> str:
"""Fetch a URL through Web Unlocker and return raw HTML."""
response = requests.post(
"https://api.brightdata.com/request",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
},
json={
"zone": UNLOCKER_ZONE,
"url": url,
"format": "raw",
},
timeout=60,
)
response.raise_for_status()
return response.text
def unlock_url_via_proxy(
url: str, customer_id: str, zone: str, password: str, ca_cert_path: str = None
) -> str:
"""Alternative: access via proxy endpoint (useful for existing HTTP clients).
For HTTPS targets, you need Bright Data's SSL certificate:
Download: https://brightdata.com/static/brightdata_proxy_ca.zip
Pass the path to ca.crt as ca_cert_path, or install it system-wide.
See docs/TROUBLESHOOTING.md for details.
"""
proxy = f"http://brd-customer-{customer_id}-zone-{zone}:{password}@brd.superproxy.io:33335"
response = requests.get(
url,
proxies={"http": proxy, "https": proxy},
verify=ca_cert_path if ca_cert_path else True,
timeout=60,
)
response.raise_for_status()
return response.text
if __name__ == "__main__":
html = unlock_url("https://www.example.com")
print(f"Received {len(html)} chars of HTML")
print(html[:500])