Skip to main content

Crate adaptive_selector

Crate adaptive_selector 

Source
Expand description

adaptive-selector — CSS selectors that survive website redesigns.

A faithful Rust port of Scrapling’s adaptive relocation engine. Save an element’s fingerprint once; when the page changes and your CSS selector breaks, relocate the element by structural similarity instead of by selector.

use adaptive_selector::{AdaptiveDocument, SimilarityThreshold};

let doc = AdaptiveDocument::parse(r#"<div class="price">$9.99</div>"#);
let saved = doc.css_first(".price").unwrap().expect("price exists").fingerprint();

// ... the site redesigns, `.price` is now `.product-price__current` ...
let doc2 = AdaptiveDocument::parse(r#"<span class="product-price__current">$9.99</span>"#);
let found = doc2.relocate(&saved, SimilarityThreshold::default());
assert_eq!(found.first().map(|e| e.text()), Some("$9.99".to_string()));

Structs§

AdaptiveDocument
A parsed HTML document with adaptive-relocation support.
AdaptiveElement
A located element: the scraper ElementRef plus its owner document’s tree.
ElementFingerprint
A serialized element fingerprint — everything the similarity scorer needs. Store it anywhere (file, DB, JSON column); it is plain data.
SimilarityThreshold
Minimum similarity (0–100) for AdaptiveDocument::relocate to accept a candidate. Scrapling’s default is 40.

Enums§

Error
Errors returned by selector parsing and relocation.

Functions§

str_ratio
Convenience for strings — mirrors SequenceMatcher(None, a, b).ratio() including difflib’s autojunk behavior on long B sequences.