← Back to recipes

Scrape and store page data

Extract page content, save it to the workspace store, and index it for full-text search.

Scraping and Extraction Beginner JavaScript scrape_and_store.js

Prerequisites

  • An open Guida workspace

Expected result

Guida stores one scraped page record in the workspace store and indexes it for search.

Source example

scrape_and_store.js

This recipe combines browser navigation, DOM extraction, workspace storage, and full-text search indexing.

// Scrape and Store — Extract data and save it
//
// This script navigates to a page, extracts structured data
// using DOM queries, and saves results to the workspace store.
// Combines browser automation, data extraction, and persistence.
// Requires an open workspace.
const targetUrl = "https://example.com";
const collection = "scraped-pages";
// Ensure we have a browser tab
await g.tabs.newBrowserTab();
g.log("Navigating to " + targetUrl + "...");
await g.nav.navigateToUrl(targetUrl);
// Extract data from the page
const title = await g.page.executeJS("document.title");
const heading = await g.dom.extractContent("h1");
const bodyText = await g.dom.extractContent("p");
// Build a structured record
const pageData = {
url: targetUrl,
title: title,
heading: heading,
excerpt: bodyText ? bodyText.substring(0, 200) : "",
scrapedAt: new Date().toISOString()
};
g.log("Extracted: " + pageData.title);
// Save to the workspace store
const key = targetUrl.replace(/[^a-zA-Z0-9]/g, "-");
g.store.put(collection, key, pageData);
g.log("Saved to store: " + collection + "/" + key);
// Index for full-text search
g.search.index({
_key: key,
_collection: collection,
title: pageData.title,
heading: pageData.heading,
excerpt: pageData.excerpt,
url: pageData.url
});
g.log("Indexed for search");
// Verify: query it back
const results = g.search.query("example");
if (results && results.length > 0) {
g.log("Search found " + results.length + " result(s)");
}