Skip to content

Commit 93a1b15

Browse files
feat(examples): add polling loop to crawl examples
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 91f25ce commit 93a1b15

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

examples/crawl/crawl_basic.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,33 @@ import { ScrapeGraphAI } from "scrapegraph-js";
44
const sgai = ScrapeGraphAI();
55

66
const startRes = await sgai.crawl.start({
7-
url: "https://example.com",
7+
url: "https://scrapegraphai.com/",
88
maxPages: 5,
99
maxDepth: 2,
1010
});
1111

1212
if (startRes.status !== "success" || !startRes.data) {
1313
console.error("Failed to start:", startRes.error);
1414
} else {
15-
console.log("Crawl started:", startRes.data.id);
16-
console.log("Status:", startRes.data.status);
15+
const crawlId = startRes.data.id;
16+
console.log("Crawl started:", crawlId);
1717

18-
const getRes = await sgai.crawl.get(startRes.data.id);
19-
console.log("\nProgress:", getRes.data?.finished, "/", getRes.data?.total);
20-
console.log("Pages:", getRes.data?.pages.map((p) => p.url));
18+
let status = startRes.data.status;
19+
while (status === "running") {
20+
await new Promise((r) => setTimeout(r, 2000));
21+
const getRes = await sgai.crawl.get(crawlId);
22+
if (getRes.status !== "success" || !getRes.data) {
23+
console.error("Failed to get status:", getRes.error);
24+
break;
25+
}
26+
status = getRes.data.status;
27+
console.log(`Progress: ${getRes.data.finished}/${getRes.data.total} - ${status}`);
28+
29+
if (status === "completed" || status === "failed") {
30+
console.log("\nPages crawled:");
31+
for (const page of getRes.data.pages) {
32+
console.log(` ${page.url} - ${page.status}`);
33+
}
34+
}
35+
}
2136
}

examples/crawl/crawl_with_formats.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ScrapeGraphAI } from "scrapegraph-js";
44
const sgai = ScrapeGraphAI();
55

66
const res = await sgai.crawl.start({
7-
url: "https://example.com",
7+
url: "https://scrapegraphai.com/",
88
formats: [
99
{ type: "markdown", mode: "reader" },
1010
{ type: "screenshot", width: 1280, height: 720 },
@@ -15,10 +15,28 @@ const res = await sgai.crawl.start({
1515
excludePatterns: ["/admin/*"],
1616
});
1717

18-
if (res.status === "success") {
19-
console.log("Crawl ID:", res.data?.id);
20-
console.log("Status:", res.data?.status);
21-
console.log("Total pages to crawl:", res.data?.total);
18+
if (res.status !== "success" || !res.data) {
19+
console.error("Failed to start:", res.error);
2220
} else {
23-
console.error("Failed:", res.error);
21+
const crawlId = res.data.id;
22+
console.log("Crawl started:", crawlId);
23+
24+
let status = res.data.status;
25+
while (status === "running") {
26+
await new Promise((r) => setTimeout(r, 2000));
27+
const getRes = await sgai.crawl.get(crawlId);
28+
if (getRes.status !== "success" || !getRes.data) {
29+
console.error("Failed to get status:", getRes.error);
30+
break;
31+
}
32+
status = getRes.data.status;
33+
console.log(`Progress: ${getRes.data.finished}/${getRes.data.total} - ${status}`);
34+
35+
if (status === "completed" || status === "failed") {
36+
console.log("\nPages crawled:");
37+
for (const page of getRes.data.pages) {
38+
console.log(` ${page.url} - ${page.status}`);
39+
}
40+
}
41+
}
2442
}

0 commit comments

Comments
 (0)