-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-extraction.ts
More file actions
51 lines (42 loc) · 1.3 KB
/
basic-extraction.ts
File metadata and controls
51 lines (42 loc) · 1.3 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
/**
* Basic Extraction Example
*
* This example demonstrates:
* - Creating a robot with captureText
* - Extracting specific fields from a single page
* - Running the robot and retrieving results
*
* Site: Hacker News (https://news.ycombinator.com)
*/
import 'dotenv/config';
import { Extract } from 'maxun-sdk';
async function main() {
const extractor = new Extract({
apiKey: process.env.MAXUN_API_KEY!,
baseUrl: process.env.MAXUN_BASE_URL!
});
try {
// Extract top story from Hacker News
const robot = await extractor
.create('Hacker News Top Story')
.navigate('https://news.ycombinator.com')
.captureText({
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 a:last-child'
});
console.log(`Robot created: ${robot.id}`);
const result = await robot.run();
console.log('\nExtracted Hacker News Top Story:');
console.log(JSON.stringify(result.data.textData, null, 2));
} catch (error: any) {
console.error('Error:', error.message);
process.exit(1);
}
}
if (!process.env.MAXUN_API_KEY) {
console.error('Error: MAXUN_API_KEY environment variable is required');
process.exit(1);
}
main();