-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_scrape.py
More file actions
39 lines (30 loc) · 1.28 KB
/
Copy pathbatch_scrape.py
File metadata and controls
39 lines (30 loc) · 1.28 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
"""Batch scraping examples using the AlterLab Python SDK."""
import asyncio
from alterlab import AlterLab
async def main():
async with AlterLab(api_key="sk_test_...") as client:
# 1. Submit a batch of URLs
batch = await client.batch_scrape(
[
{"url": "https://example.com/page1", "mode": "html"},
{"url": "https://example.com/page2", "mode": "html"},
{"url": "https://example.com/page3", "mode": "js"},
],
webhook_url="https://myapp.com/webhook", # optional
)
print(f"Batch ID: {batch['batch_id']}")
print(f"Jobs: {len(batch['job_ids'])}")
# 2. Wait for all jobs to complete
results = []
for job_id in batch["job_ids"]:
result = await client.wait_for_job(job_id, poll_timeout=60.0)
results.append(result)
print(f" Job {job_id}: done")
total_credits = sum(r["billing"]["total_credits"] for r in results)
print(f"Total credits: {total_credits}")
# 3. Or check status without waiting
for job_id in batch["job_ids"]:
status = await client.get_job_status(job_id)
print(f" Job {job_id}: {status['status']}")
if __name__ == "__main__":
asyncio.run(main())