Skip to content

API Reference parrot_tools.scraping.page_snapshot

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.scraping.page_snapshot

View source

Page snapshot builder for LLM-based plan generation.

Produces a compact textual summary of a target page that the PlanGenerator feeds into the prompt so the LLM can choose real selectors instead of guessing ([data-testid='...'], .plan-card).

The key piece is structure — a pruned DOM outline where repeating siblings are collapsed to a single exemplar plus a (×N more identical) marker. This exposes the repeating-block patterns (card carousels, FAQ accordions) the LLM needs to pick a precise row selector.

Two fetch strategies are provided:

  • fetch_snapshot (default): a lightweight aiohttp GET. Fast and cheap; suitable for server-rendered pages. Misses JS-hydrated content.
  • snapshot_from_html: accepts raw HTML (e.g. already captured via a browser driver) and builds the snapshot without any network call.

Attribute CHROME_ID_CLASS_PATTERNS

Attribute CHROME_ROLES

Attribute CHROME_TAGS

Attribute DEFAULT_UA

Attribute MAX_ATTR_LEN

Attribute MAX_ELEMENT_HINTS

Attribute MAX_LINKS

Class PageSnapshot

Compact page data for LLM prompt building.

Fields are plain strings so they interpolate cleanly into the PLAN_GENERATION_PROMPT template. A Pydantic model so it can be exposed as a tool parameter (WebScrapingToolkit.plan_create) with a proper JSON schema.

Fields:

Field Type Default Description
title str '' Page <title> or og:title.
text_excerpt str '' First ~2000 chars of visible text.
element_hints str '' Newline-separated list of notable elements with their tag, id, class, data-, aria-, and role attributes.
structure str '' Pruned DOM outline (indented, repetition-collapsed) showing the repeating-block patterns the LLM should anchor its selectors on.
links str '' Newline-separated text -> href pairs (up to 50).

Attribute STRUCTURE_INTERESTING_TAGS

Attribute STRUCTURE_MAX_CHARS

Attribute STRUCTURE_MAX_DEPTH

Attribute STRUCTURE_NODE_TEXT_CHARS

Attribute STRUCTURE_REPEAT_SHOW

Attribute STRUCTURE_SKIP_TAGS

Attribute STRUCTURE_TRANSPARENT_CLASS_MARKERS

Attribute TEXT_EXCERPT_CHARS

Function fetch_snapshot

Fetch a URL via aiohttp and build a PageSnapshot.

Returns None on any fetch failure — plan generation should then fall back to an empty snapshot rather than crashing. JS-rendered pages will produce a sparse snapshot; capture HTML via the browser driver and call snapshot_from_html directly for those.

fetch_snapshot(url: str, *, timeout: float = 10.0, user_agent: str = DEFAULT_UA, session: Optional[aiohttp.ClientSession] = None) -> Optional[PageSnapshot]

Parameter Type Default Description
url str Target URL to fetch.
timeout float 10.0 Request timeout in seconds.
user_agent str DEFAULT_UA User-Agent header.
session Optional[aiohttp.ClientSession] None Optional pre-existing aiohttp.ClientSession to reuse.

Returns: Optional[PageSnapshot] — A PageSnapshot or None if the fetch failed.

Attribute logger

Function snapshot_from_driver

Build a PageSnapshot from a live AbstractDriver (Selenium or Playwright).

The driver should already have navigated to the target URL — we just read driver.get_page_source() here so the snapshot reflects the post-hydration DOM (i.e. what the user actually sees on SPA pages like React/Next.js/Vue apps where server HTML is mostly empty).

When scroll_sweep is True (default), the page is scrolled top→bottom in ~4 chunks before snapshotting so intersection-observer -driven content (below-fold carousels, FAQ accordions) hydrates.

Use this in preference to fetch_snapshot whenever a driver is available: most modern sites hydrate content client-side, making aiohttp-based snapshots sparse and misleading to the LLM.

snapshot_from_driver(driver: Any, url: Optional[str] = None, *, settle_seconds: float = 1.0, scroll_sweep: bool = True) -> Optional[PageSnapshot]

Parameter Type Default Description
driver Any Live AbstractDriver (Selenium or Playwright), already on the target page.
url Optional[str] None Optional URL to navigate to before snapshotting. If provided and the driver's current URL differs, driver.navigate(url) is called first.
settle_seconds float 1.0 Grace period after navigation to let async content hydrate. Default 1.0s — adjust up for heavy SPA pages.
scroll_sweep bool True Scroll top→bottom to force below-fold hydration.

Returns: Optional[PageSnapshot] — Populated PageSnapshot, or None if the driver call fails.

Function snapshot_from_html

Build a PageSnapshot from raw HTML without any network call.

snapshot_from_html(html: str) -> PageSnapshot

Parameter Type Default Description
html str Raw HTML document.

Returns: PageSnapshot — Populated PageSnapshot.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally