Build a declarative scraper

Define a reusable scraper with reliable CSS, XPath, or JSON extraction rules.

Build a declarative scraper

A declarative scraper describes its inputs, target, and structured extraction
rules without application code. Use one when an available Raspbytes-managed
scraper does not match the target or required output.

The scraper is the reusable product object. Its output fields are mappings that
describe individual values such as names, prices, dates, ratings, availability,
or profile details.

Start with the output

Define the desired record before inspecting selectors:

{
  "name": "Wireless Headphones",
  "price": "£79.99",
  "availability": "In stock",
  "image_url": "https://example.com/images/headphones.jpg"
}

Use stable, descriptive field names. A field name begins with a letter or
underscore and may contain letters, numbers, underscores, or hyphens.

Define output fields

Put output fields under extractor.fields. Use source to select CSS, XPath,
or JSON extraction; use selector to locate the value. When attribute is
omitted, HTML fields return text. Set multiple to true to return every
matching value as an array.

FieldPurpose
sourceExtraction syntax: css, xpath, or json.
selectorExpression locating the value.
attributeOptional HTML attribute to return instead of text.
multipleReturn all matches as an array when true.
{
  "url": "https://example.com/products/wireless-headphones",
  "extractor": {
    "fields": {
      "name": {
        "source": "css",
        "selector": "h1.product-name"
      },
      "price": {
        "source": "css",
        "selector": ".product-price"
      },
      "image_url": {
        "source": "css",
        "selector": ".product-gallery img",
        "attribute": "src"
      }
    }
  }
}

Prefer semantic IDs, stable classes, data-* attributes, and meaningful HTML
relationships. Avoid generated class names and deeply positional selectors.

Extract repeated records

Use record_selector for pages containing repeated items. Field selectors are
then evaluated relative to each matched record, preventing values from different
cards from being combined.

{
  "url": "https://example.com/products",
  "extractor": {
    "record_selector": {
      "source": "css",
      "selector": ".product-card",
      "limit": 50
    },
    "fields": {
      "name": { "source": "css", "selector": ".product-name" },
      "price": { "source": "css", "selector": ".product-price" },
      "product_url": {
        "source": "css",
        "selector": "a.product-link",
        "attribute": "href"
      }
    }
  }
}

Use XPath or JSON when appropriate

CSS is generally the simplest choice. XPath is useful when selection depends on
hierarchy or complex relationships. Inside a repeated record, begin relative
XPath selectors with .//.

{
  "record_selector": {
    "source": "xpath",
    "selector": "//article[contains(@class, 'product-card')]"
  },
  "fields": {
    "name": { "source": "xpath", "selector": ".//h2" },
    "price": {
      "source": "xpath",
      "selector": ".//span[contains(@class, 'price')]"
    }
  }
}

Use source: "json" for page-level structured JSON. JSON fields are not used
with an HTML record_selector.

{
  "fields": {
    "product_name": {
      "source": "json",
      "selector": "product.name"
    },
    "price": {
      "source": "json",
      "selector": "product.offers.price"
    }
  }
}

Add pagination

Pagination follows one same-host next-page link at a time. It requires repeated
records and accepts a bounded page limit that includes the first page.

{
  "record_selector": {
    "source": "css",
    "selector": ".product-card"
  },
  "fields": {
    "name": { "source": "css", "selector": ".product-name" },
    "price": { "source": "css", "selector": ".product-price" }
  },
  "pagination": {
    "source": "css",
    "selector": "a.next-page",
    "attribute": "href",
    "limit": 5
  }
}

Follow detail pages

Use follow when a listing contains summary cards while the full record lives
on each detail page. The link selector runs relative to the listing record;
follow.fields runs from the detail-page root. A detail field replaces a
listing field with the same name.

{
  "record_selector": {
    "source": "css",
    "selector": ".product-card"
  },
  "fields": {
    "name": { "source": "css", "selector": ".product-name" },
    "product_url": {
      "source": "css",
      "selector": "a.product-link",
      "attribute": "href"
    }
  },
  "follow": {
    "source": "css",
    "selector": "a.product-link",
    "attribute": "href",
    "limit": 20,
    "fields": {
      "description": {
        "source": "css",
        "selector": ".product-description"
      },
      "sku": {
        "source": "css",
        "selector": "[data-product-sku]",
        "attribute": "data-product-sku"
      }
    }
  }
}

Understand content hidden behind See more

Some pages truncate a field until See more, View details, or a similar
control is selected. The interaction may expand content inside the record or
open a modal, drawer, or other panel on the same page. This is different from
follow, which navigates to another URL.

Raspbytes models the same-page interaction as a reveal:

  • the trigger belongs to one repeated record;
  • record-scoped content covers inline and in-card expansion;
  • document-scoped content covers modals, drawers, and portal-rendered panels;
  • field selectors run inside the revealed content root;
  • the revealed fields are merged into the originating output record rather
    than returned as a separate modal or drawer object;
  • an optional restore action closes or collapses the state before the next
    record is processed.

When a revealed value replaces a truncated listing value, the scraper uses an
explicit merge rule. Other revealed fields can be added to the same
record as long as they are declared in its output schema. If the interaction
does not complete, the original listing value is preserved and the run returns
a record-level warning.

Customer declarative scrapers define reveals under extractor.reveals. Set
render_js to true; reveal actions require browser retrieval and a repeated
record selector.

{
  "url": "https://example.com/jobs",
  "method": "GET",
  "render_js": true,
  "response_format": "json",
  "extractor": {
    "record_selector": {
      "source": "css",
      "selector": "article.job",
      "limit": 25
    },
    "fields": {
      "title": { "source": "css", "selector": "h2" },
      "description": { "source": "css", "selector": ".summary" }
    },
    "reveals": [
      {
        "id": "complete-details",
        "trigger": {
          "source": "css",
          "selector": "button[data-action='show-details']"
        },
        "content": {
          "scope": "document",
          "source": "css",
          "selector": "[role='dialog'][data-job-details]"
        },
        "restore": {
          "source": "css",
          "selector": "button[aria-label='Close']"
        },
        "fields": {
          "description": {
            "source": "css",
            "selector": ".full-description",
            "merge": "replace_if_present"
          },
          "requirements": {
            "source": "css",
            "selector": ".requirement",
            "multiple": true
          }
        },
        "timeout_ms": 3000,
        "limit": 25
      }
    ]
  }
}

Use scope: "record" when the content expands inside the record. Use
scope: "document" for a modal, drawer, or portal mounted elsewhere on the
page, and add restore when it must be closed before processing the next
record. Reveal selectors support CSS and XPath, not JavaScript.

Each scraper may define at most five reveals. timeout_ms must be between 100
and 5,000 milliseconds and limit between 1 and 100. A reveal that writes an
existing field must choose replace_if_present, fill_missing, or
append_unique; the latter requires multiple: true. A field cannot be
written by both a reveal and follow.

Render JavaScript only when required

Enable render_js when required elements appear only after client-side
rendering. Standard retrieval is usually faster when the data already exists in
the returned HTML.

{
  "url": "https://example.com/dynamic-products",
  "method": "GET",
  "render_js": true,
  "follow_redirects": true,
  "extractor": {
    "record_selector": {
      "source": "css",
      "selector": ".product-card"
    },
    "fields": {
      "name": { "source": "css", "selector": ".product-name" },
      "price": { "source": "css", "selector": ".product-price" }
    }
  }
}

Handle optional values and retries

A missing field does not necessarily fail a run. A single-value field may return
null, a multiple field may return an empty array, and warnings identify the
missing value or affected record.

Choose JSON for software integrations or Markdown for readable and language
model workflows. Response format changes presentation, not extraction.

A normal new request performs a fresh retrieval. An explicit idempotency key
makes a retry replay the original request outcome; omit it when every submission
must collect the page again.

Preparation checklist

  • Define the required record and distinguish optional fields.
  • Test selectors against representative pages, empty states, and variants.
  • Prefer stable semantic attributes over layout position.
  • Start with a small field set.
  • Add repeated records, pagination, detail pages, or rendering only when needed.
  • Use a bounded reveal for same-page See more content; use follow for a
    different URL.
  • Keep page and follow limits bounded.
  • Handle warnings, null, and empty arrays in the consumer.
  • Pin the working scraper revision in a Scraping Task before scheduling it.

Complete example

This request extracts a rendered listing, follows pagination, and visits each
detail page for additional fields:

{
  "url": "https://example.com/products",
  "method": "GET",
  "render_js": true,
  "response_format": "json",
  "extractor": {
    "record_selector": {
      "source": "css",
      "selector": ".product-card",
      "limit": 50
    },
    "fields": {
      "name": { "source": "css", "selector": ".product-name" },
      "price": { "source": "css", "selector": ".product-price" },
      "product_url": {
        "source": "css",
        "selector": "a.product-link",
        "attribute": "href"
      }
    },
    "pagination": {
      "source": "css",
      "selector": "a.next-page",
      "attribute": "href",
      "limit": 5
    },
    "follow": {
      "source": "css",
      "selector": "a.product-link",
      "attribute": "href",
      "limit": 25,
      "fields": {
        "description": {
          "source": "css",
          "selector": ".product-description"
        },
        "image_url": {
          "source": "css",
          "selector": ".product-gallery img",
          "attribute": "src"
        },
        "availability": {
          "source": "css",
          "selector": ".availability"
        }
      }
    }
  }
}

Validate a small field set against representative pages first. Add repeated
records, pagination, rendering, and detail-page following one capability at a
time.


Did this page help you?