forked from brightdata/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_unlocker.py
More file actions
463 lines (410 loc) · 16.1 KB
/
web_unlocker.py
File metadata and controls
463 lines (410 loc) · 16.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""Web Unlocker API - High-level service wrapper for Bright Data's Web Unlocker proxy service.
All methods are async-only. For sync usage, use SyncBrightDataClient.
"""
from typing import Union, List, Optional, Dict, Any
from datetime import datetime, timezone
import asyncio
from .base import BaseAPI
from .async_unblocker import AsyncUnblockerClient
from ..models import ScrapeResult
from ..utils.validation import (
validate_url,
validate_url_list,
validate_zone_name,
validate_country_code,
validate_timeout,
validate_response_format,
validate_http_method,
)
from ..utils.url import extract_root_domain
from ..utils.function_detection import get_caller_function_name
from ..constants import HTTP_OK
from ..exceptions import ValidationError, APIError
class WebUnlockerService(BaseAPI):
"""
High-level service wrapper around Bright Data's Web Unlocker proxy service.
Provides simple HTTP-based scraping with anti-bot capabilities. This is the
fastest, most cost-effective option for basic HTML extraction without JavaScript rendering.
Example:
>>> async with AsyncEngine(token) as engine:
... service = WebUnlockerService(engine)
... result = await service.scrape_async("https://example.com", zone="my_zone")
... print(result.data)
"""
ENDPOINT = "/request"
def __init__(self, engine):
"""
Initialize Web Unlocker service.
Args:
engine: AsyncEngine instance for HTTP operations
"""
super().__init__(engine)
# Initialize async unblocker client for async mode support
self.async_unblocker = AsyncUnblockerClient(engine)
async def _execute_async(self, *args: Any, **kwargs: Any) -> Any:
"""Execute API operation asynchronously."""
return await self.scrape_async(*args, **kwargs)
async def scrape_async(
self,
url: Union[str, List[str]],
zone: str,
country: str = "",
response_format: str = "raw",
method: str = "GET",
timeout: Optional[int] = None,
mode: str = "sync",
poll_interval: int = 2,
poll_timeout: int = 180,
) -> Union[ScrapeResult, List[ScrapeResult]]:
"""
Scrape URL(s) asynchronously using Web Unlocker API.
Args:
url: Single URL string or list of URLs to scrape.
zone: Bright Data zone identifier.
country: Two-letter ISO country code for proxy location (optional).
response_format: Response format - "json" for structured data, "raw" for HTML string.
method: HTTP method for the request (default: "GET").
timeout: Request timeout in seconds (uses engine default if not provided).
mode: "sync" (default, blocking) or "async" (non-blocking with polling).
poll_interval: Seconds between polls (async mode only, default: 2).
poll_timeout: Max wait time in seconds (async mode only, default: 180).
Warning:
Web Unlocker async mode takes ~145 seconds to complete. For faster results,
use sync mode (default). See devdocs/web_unlocker_async_inspection.md.
Returns:
ScrapeResult for single URL, or List[ScrapeResult] for multiple URLs.
Raises:
ValidationError: If input validation fails.
APIError: If API request fails.
Note:
- Sync mode (default): Uses /request endpoint, blocks until results ready
- Async mode: Uses /unblocker/req + /unblocker/get_result, polls for results
- Both modes return the same ScrapeResult structure
"""
validate_zone_name(zone)
validate_response_format(response_format)
validate_http_method(method)
validate_country_code(country)
if timeout is not None:
validate_timeout(timeout)
# Route based on mode
if mode == "async":
# Async mode: use unblocker endpoints with polling
if isinstance(url, list):
validate_url_list(url)
return await self._scrape_multiple_async_unblocker(
urls=url,
zone=zone,
country=country,
response_format=response_format,
method=method,
poll_interval=poll_interval,
poll_timeout=poll_timeout,
)
else:
validate_url(url)
return await self._scrape_single_async_unblocker(
url=url,
zone=zone,
country=country,
response_format=response_format,
method=method,
poll_interval=poll_interval,
poll_timeout=poll_timeout,
)
else:
# Sync mode (default): use /request endpoint (existing behavior)
if isinstance(url, list):
validate_url_list(url)
return await self._scrape_multiple_async(
urls=url,
zone=zone,
country=country,
response_format=response_format,
method=method,
timeout=timeout,
)
else:
validate_url(url)
return await self._scrape_single_async(
url=url,
zone=zone,
country=country,
response_format=response_format,
method=method,
timeout=timeout,
)
async def _scrape_single_async(
self,
url: str,
zone: str,
country: str,
response_format: str,
method: str,
timeout: Optional[int],
) -> ScrapeResult:
"""Scrape a single URL."""
trigger_sent_at = datetime.now(timezone.utc)
payload: Dict[str, Any] = {
"zone": zone,
"url": url,
"format": response_format,
"method": method,
}
if country:
payload["country"] = country.upper()
sdk_function = get_caller_function_name()
if sdk_function:
payload["sdk_function"] = sdk_function
try:
# Make the request and read response body immediately
async with self.engine.post_to_url(
f"{self.engine.BASE_URL}{self.ENDPOINT}", json_data=payload
) as response:
data_fetched_at = datetime.now(timezone.utc)
if response.status == HTTP_OK:
if response_format == "json":
try:
data = await response.json()
except (ValueError, TypeError) as e:
raise APIError(f"Failed to parse JSON response: {str(e)}")
else:
data = await response.text()
root_domain = extract_root_domain(url)
html_char_size = len(data) if isinstance(data, str) else None
return ScrapeResult(
success=True,
url=url,
status="ready",
data=data,
cost=None,
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=data_fetched_at,
root_domain=root_domain,
html_char_size=html_char_size,
)
else:
error_text = await response.text()
return ScrapeResult(
success=False,
url=url,
status="error",
error=f"API returned status {response.status}: {error_text}",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=data_fetched_at,
)
except Exception as e:
data_fetched_at = datetime.now(timezone.utc)
if isinstance(e, (ValidationError, APIError)):
raise
return ScrapeResult(
success=False,
url=url,
status="error",
error=f"Unexpected error: {str(e)}",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=data_fetched_at,
)
async def _scrape_multiple_async(
self,
urls: List[str],
zone: str,
country: str,
response_format: str,
method: str,
timeout: Optional[int],
) -> List[ScrapeResult]:
"""Scrape multiple URLs concurrently."""
tasks = [
self._scrape_single_async(
url=url,
zone=zone,
country=country,
response_format=response_format,
method=method,
timeout=timeout,
)
for url in urls
]
results = await asyncio.gather(*tasks, return_exceptions=True)
processed_results: List[ScrapeResult] = []
for i, result in enumerate(results):
if isinstance(result, Exception):
processed_results.append(
ScrapeResult(
success=False,
url=urls[i],
status="error",
error=f"Exception: {str(result)}",
trigger_sent_at=datetime.now(timezone.utc),
data_fetched_at=datetime.now(timezone.utc),
)
)
else:
processed_results.append(result)
return processed_results
async def _scrape_single_async_unblocker(
self,
url: str,
zone: str,
country: str,
response_format: str,
method: str,
poll_interval: int,
poll_timeout: int,
) -> ScrapeResult:
"""
Scrape single URL using async unblocker endpoints.
This method:
1. Triggers async request via /unblocker/req
2. Polls /unblocker/get_result until ready or timeout
3. Fetches and returns scraped content
"""
trigger_sent_at = datetime.now(timezone.utc)
# Trigger async request
try:
response_id = await self.async_unblocker.trigger(
zone=zone,
url=url,
format=response_format,
method=method,
country=country.upper() if country else None,
)
except Exception as e:
return ScrapeResult(
success=False,
url=url,
status="error",
error=f"Failed to trigger async request: {str(e)}",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=datetime.now(timezone.utc),
)
if not response_id:
return ScrapeResult(
success=False,
url=url,
status="error",
error="Failed to trigger async request (no response_id received)",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=datetime.now(timezone.utc),
)
# Poll until ready or timeout
start_time = datetime.now(timezone.utc)
while True:
elapsed = (datetime.now(timezone.utc) - start_time).total_seconds()
# Check timeout
if elapsed > poll_timeout:
return ScrapeResult(
success=False,
url=url,
status="timeout",
error=f"Polling timeout after {poll_timeout}s (response_id: {response_id})",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=datetime.now(timezone.utc),
)
# Check status
try:
status = await self.async_unblocker.get_status(zone, response_id)
except Exception as e:
return ScrapeResult(
success=False,
url=url,
status="error",
error=f"Failed to check status: {str(e)}",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=datetime.now(timezone.utc),
)
if status == "ready":
# Results ready - fetch them
data_fetched_at = datetime.now(timezone.utc)
try:
data = await self.async_unblocker.fetch_result(
zone, response_id, response_format=response_format
)
root_domain = extract_root_domain(url)
html_char_size = len(data) if isinstance(data, str) else None
return ScrapeResult(
success=True,
url=url,
status="ready",
data=data,
cost=None,
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=data_fetched_at,
root_domain=root_domain,
html_char_size=html_char_size,
)
except Exception as e:
return ScrapeResult(
success=False,
url=url,
status="error",
error=f"Failed to fetch results: {str(e)}",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=data_fetched_at,
)
elif status == "error":
return ScrapeResult(
success=False,
url=url,
status="error",
error=f"Async request failed (response_id: {response_id})",
method="web_unlocker",
trigger_sent_at=trigger_sent_at,
data_fetched_at=datetime.now(timezone.utc),
)
# Still pending - wait and retry
await asyncio.sleep(poll_interval)
async def _scrape_multiple_async_unblocker(
self,
urls: List[str],
zone: str,
country: str,
response_format: str,
method: str,
poll_interval: int,
poll_timeout: int,
) -> List[ScrapeResult]:
"""Execute multiple scrapes using async unblocker."""
tasks = [
self._scrape_single_async_unblocker(
url=url,
zone=zone,
country=country,
response_format=response_format,
method=method,
poll_interval=poll_interval,
poll_timeout=poll_timeout,
)
for url in urls
]
# Execute all scrapes concurrently
results = await asyncio.gather(*tasks, return_exceptions=True)
# Process results, converting exceptions to ScrapeResult errors
processed_results: List[ScrapeResult] = []
for i, result in enumerate(results):
if isinstance(result, Exception):
processed_results.append(
ScrapeResult(
success=False,
url=urls[i],
status="error",
error=f"Exception: {str(result)}",
method="web_unlocker",
trigger_sent_at=datetime.now(timezone.utc),
data_fetched_at=datetime.now(timezone.utc),
)
)
else:
processed_results.append(result)
return processed_results
scrape = scrape_async