-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathasync_extract_example.py
More file actions
40 lines (29 loc) · 948 Bytes
/
async_extract_example.py
File metadata and controls
40 lines (29 loc) · 948 Bytes
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
"""
Async extract example - extract data from multiple pages concurrently.
"""
import asyncio
import json
from pydantic import BaseModel, Field
from scrapegraph_py import AsyncClient
class PageInfo(BaseModel):
title: str = Field(description="Page title")
description: str = Field(description="Brief description of the page content")
async def main():
async with AsyncClient() as client:
urls = [
"https://example.com",
"https://httpbin.org/html",
]
tasks = [
client.extract(
url=url,
prompt="Extract the page title and a brief description",
output_schema=PageInfo,
)
for url in urls
]
results = await asyncio.gather(*tasks)
for url, result in zip(urls, results):
print(f"\n=== {url} ===")
print(json.dumps(result, indent=2))
asyncio.run(main())