-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscrape_json_extraction.ts
More file actions
42 lines (37 loc) · 1.05 KB
/
scrape_json_extraction.ts
File metadata and controls
42 lines (37 loc) · 1.05 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
import { ScrapeGraphAI } from "scrapegraph-js";
// reads SGAI_API_KEY from env, or pass explicitly: ScrapeGraphAI({ apiKey: "..." })
const sgai = ScrapeGraphAI();
const res = await sgai.scrape({
url: "https://example.com",
formats: [
{
type: "json",
prompt: "Extract the company name, tagline, and list of features",
schema: {
type: "object",
properties: {
companyName: { type: "string" },
tagline: { type: "string" },
features: {
type: "array",
items: { type: "string" },
},
},
required: ["companyName"],
},
},
],
});
if (res.status === "success") {
const json = res.data?.results.json;
console.log("=== JSON Extraction ===\n");
console.log("Extracted data:");
console.log(JSON.stringify(json?.data, null, 2));
if (json?.metadata?.chunker) {
console.log("\nChunker info:");
console.log(" Chunks:", json.metadata.chunker.chunks.length);
console.log(" Total size:", json.metadata.chunker.chunks.reduce((a, c) => a + c.size, 0), "chars");
}
} else {
console.error("Failed:", res.error);
}