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:- Saving element signatures (tag, attributes, text, parent structure, siblings)
- Storing these signatures with an identifier
- Relocating elements when selectors fail by comparing stored signatures with current elements
- Scoring candidates based on similarity percentage
- 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
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
Bothcss() 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.
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
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
tag: Element tag nametext: Element text contentattributes: Element attributespath: Element’s path in the DOM treeparent_name: Parent element’s tag nameparent_attribs: Parent element’s attributesparent_text: Parent element’s textsiblings: 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.
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
- Tag Name Match (exact match)
- Text Similarity (using SequenceMatcher)
- Attributes Similarity (keys and values)
- Class, ID, Href, Src (separate scoring for important attributes)
- Path Similarity (DOM tree path)
- Parent Structure (parent tag, attributes, text)
- 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 extendingStorageSystemMixin:
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)