Skip to main content

Overview

Adaptive parsing is Scrapling’s innovative feature that makes your scrapers resilient to website structure changes. Instead of breaking when a website updates its HTML, Scrapling can automatically relocate elements based on their unique characteristics.
Adaptive scraping uses a similarity algorithm to match elements even when selectors change, making your scrapers more maintainable and reliable.

How It Works

Adaptive parsing works by:
  1. Saving element signatures (tag, attributes, text, parent structure, siblings)
  2. Storing these signatures with an identifier
  3. Relocating elements when selectors fail by comparing stored signatures with current elements
  4. Scoring candidates based on similarity percentage
  5. Returning the best matches above a threshold

Enabling Adaptive Mode

Enable adaptive parsing when creating a Selector:
bool
default:"false"
Globally enable adaptive features for all selector methods
StorageSystemMixin
default:"SQLiteStorageSystem"
The storage class to use for saving element signatures. Must be wrapped with lru_cache decorator.
Dict
Arguments to pass to the storage class constructor
The adaptive parameter must be set during initialization. It cannot be changed later and takes priority over all adaptive-related arguments in selector methods.

Basic Usage

Auto-Save Mode

Automatically save element signatures when first found:

Manual Save and Retrieve

Explicitly control when to save and retrieve:

Selector Methods with Adaptive Support

Both css() and xpath() support adaptive parameters.

css() with Adaptive

str
default:""
Unique identifier for saving/retrieving element data. If not provided, the selector string is used.
Always use explicit identifiers when you plan to change selectors in the future
bool
default:"false"
Enable adaptive relocation for this specific selector call
bool
default:"false"
Automatically save the first matched element with the identifier
int
default:"0"
Minimum similarity percentage required when relocating (0-100). Higher values are more strict.

xpath() with Adaptive

Accepts the same adaptive parameters as css(), plus:
Any
Additional keyword arguments passed as XPath variables

Core Adaptive Methods

save()

Save an element’s signature to storage.
HtmlElement | Selector
required
The element to save. Can be a Selector or raw HtmlElement.
str
required
Unique identifier for retrieving the element later

retrieve()

Retrieve a saved element’s signature from storage.
str
required
The identifier used when saving the element
Returns a dictionary containing:
  • tag: Element tag name
  • text: Element text content
  • attributes: Element attributes
  • path: Element’s path in the DOM tree
  • parent_name: Parent element’s tag name
  • parent_attribs: Parent element’s attributes
  • parent_text: Parent element’s text
  • siblings: Information about sibling elements

relocate()

Find elements matching a saved signature.
Dict | HtmlElement | Selector
required
The element signature to search for. Usually a dictionary from retrieve().
int
default:"0"
Minimum similarity percentage (0-100). Only elements scoring above this are returned.
The percentage calculation depends on page structure. Start with low values (0-30) and increase if needed.
bool
default:"false"
If True, return results as Selectors object instead of raw HtmlElement list

Similarity Scoring

Scrapling calculates similarity based on multiple factors:

Scoring Factors

  1. Tag Name Match (exact match)
  2. Text Similarity (using SequenceMatcher)
  3. Attributes Similarity (keys and values)
  4. Class, ID, Href, Src (separate scoring for important attributes)
  5. Path Similarity (DOM tree path)
  6. Parent Structure (parent tag, attributes, text)
  7. Siblings Information (surrounding elements)

How Similarity is Calculated

Practical Examples

Example 1: Product Scraper

Example 2: News Article Scraper

Example 3: Monitoring Website Changes

Example 4: Multi-Page Scraper with Adaptive

Best Practices

Use Descriptive Identifiers

Use clear, versioned identifiers like 'product-price-v1' instead of relying on selectors as identifiers.

Start with Auto-Save

Use auto_save=True during development to automatically build your element database.

Tune Percentage Carefully

Start with low percentage values (0-30) and increase only if you get too many false positives.

Monitor Adaptive Usage

Enable debug logging to see when adaptive mode is being used vs. direct selectors.

Custom Storage Backend

You can implement custom storage backends by extending StorageSystemMixin:
Custom storage classes must:
  1. Be wrapped with @lru_cache decorator
  2. Inherit from StorageSystemMixin
  3. Accept url parameter

Limitations

  • Adaptive mode requires elements to have been saved before relocation
  • Very similar elements on the same page may cause false positives
  • Completely restructured pages may fall below similarity thresholds
  • Text nodes cannot be saved (their parent element is saved instead)

Troubleshooting

Element Not Found Even with Adaptive

Too Many False Positives

Updating Saved Elements