-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbasic-scrape.ts
More file actions
53 lines (43 loc) · 1.55 KB
/
basic-scrape.ts
File metadata and controls
53 lines (43 loc) · 1.55 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
#!/usr/bin/env node
/**
* Basic Scraping Example
*
* Demonstrates simple single-URL scraping with reader
*/
import { ReaderClient } from "@vakra-dev/reader";
async function main() {
console.log("Starting basic scrape example\n");
const reader = new ReaderClient({ verbose: true });
try {
const result = await reader.scrape({
urls: ["https://example.com"],
formats: ["markdown", "html"],
});
const page = result.data[0];
if (!page) {
console.error("No data returned - scrape may have failed");
console.log("Errors:", result.batchMetadata.errors);
process.exit(1);
}
console.log("\nScrape completed!");
console.log("\nResults:");
console.log(` URL: ${page.metadata.baseUrl}`);
console.log(` Title: ${page.metadata.website.title}`);
console.log(` Duration: ${page.metadata.duration}ms`);
console.log(` Markdown length: ${page.markdown?.length || 0} chars`);
console.log(` HTML length: ${page.html?.length || 0} chars`);
console.log("\nMarkdown Preview (first 500 chars):");
console.log(page.markdown?.slice(0, 500));
console.log("\nBatch Metadata:");
console.log(` Total URLs: ${result.batchMetadata.totalUrls}`);
console.log(` Successful: ${result.batchMetadata.successfulUrls}`);
console.log(` Failed: ${result.batchMetadata.failedUrls}`);
console.log(` Total Duration: ${result.batchMetadata.totalDuration}ms`);
} catch (error: any) {
console.error("Error:", error.message);
process.exit(1);
} finally {
await reader.close();
}
}
main();