-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_data.py
More file actions
56 lines (46 loc) · 1.82 KB
/
Copy pathextract_data.py
File metadata and controls
56 lines (46 loc) · 1.82 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
"""Structured data extraction 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. Use a pre-built extraction profile
result = await client.scrape(
"https://example.com/product",
extraction_profile="product",
)
data = result.get("structured_content", {})
print(f"Product: {data.get('name')}")
print(f"Price: {data.get('price')}")
# 2. Custom JSON Schema extraction
schema = {
"type": "object",
"properties": {
"title": {"type": "string"},
"price": {"type": "number"},
"currency": {"type": "string"},
"in_stock": {"type": "boolean"},
},
"required": ["title", "price"],
}
result = await client.scrape(
"https://example.com/product",
extraction_schema=schema,
)
print(f"Extracted: {result.get('structured_content')}")
# 3. Natural language extraction prompt
result = await client.scrape(
"https://example.com/article",
extraction_prompt="Extract the article title, author, and publication date",
)
print(f"Extracted: {result.get('structured_content')}")
# 4. Extraction with evidence (provenance tracking)
result = await client.scrape(
"https://example.com/product",
extraction_profile="product",
evidence=True,
)
print(f"Data: {result.get('structured_content')}")
print(f"Method: {result.get('extraction_method')}")
# Available profiles: auto, product, article, job_posting, faq, recipe, event
if __name__ == "__main__":
asyncio.run(main())