-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_extraction.py
More file actions
56 lines (41 loc) · 1.35 KB
/
basic_extraction.py
File metadata and controls
56 lines (41 loc) · 1.35 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
"""
Basic Extraction Example
This example demonstrates:
- Creating a robot with capture_text
- Extracting specific fields from a single page
- Running the robot and retrieving results
Site: Hacker News (https://news.ycombinator.com)
"""
import asyncio
import json
import os
import sys
from dotenv import load_dotenv
from maxun import Extract, Config
async def main():
extractor = Extract(Config(
api_key=os.environ["MAXUN_API_KEY"],
base_url=os.environ.get("MAXUN_BASE_URL", "https://app.maxun.dev/api/sdk/"),
))
# Extract top story from Hacker News
robot = await (
extractor
.create("Hacker News Top Story")
.navigate("https://news.ycombinator.com")
.capture_text({
"Title": "tr.athing:first-child .titleline > a",
"Points": "tr.athing:first-child + tr .score",
"Author": "tr.athing:first-child + tr .hnuser",
"Posted": "tr.athing:first-child + tr .age a",
})
)
print(f"Robot created: {robot.id}")
result = await robot.run()
print(result)
print("\nExtracted Hacker News Top Story:")
print(json.dumps(result.get("data", {}).get("textData"), indent=2))
load_dotenv()
if not os.environ.get("MAXUN_API_KEY"):
print("Error: MAXUN_API_KEY environment variable is required", file=sys.stderr)
sys.exit(1)
asyncio.run(main())