Module query

Module query 

Source
Expand description

Query engine for finding elements in the DOM.

This module provides various ways to query the DOM tree:

  • CSS Selectors: Standard CSS selector syntax via the selectors crate
  • Filters: BeautifulSoup-style attribute filtering

§CSS Selectors

Use find and find_all to query by CSS selector:

use scrape_core::{
    Html5everParser, Parser,
    query::{find, find_all},
};

let parser = Html5everParser;
let doc = parser.parse("<div class=\"item\"><span>A</span></div>").unwrap();

// Find first element matching selector
let span = find(&doc, "div.item span").unwrap();

// Find all matching elements
let items = find_all(&doc, ".item").unwrap();

§Attribute Filters

Use Filter for BeautifulSoup-style queries:

use scrape_core::{
    Html5everParser, Parser,
    query::{Filter, find_by_filter},
};

let parser = Html5everParser;
let doc = parser.parse("<div class=\"item\" data-id=\"123\">text</div>").unwrap();

let filter = Filter::new().tag("div").class("item").attr("data-id", "123");

let results = find_by_filter(&doc, &filter);

§Supported CSS Selectors

SelectorExampleDescription
TypedivMatches elements by tag name
Class.fooMatches elements with class
ID#barMatches element by ID
Universal*Matches all elements
Attribute[href]Matches elements with attribute
Attribute value[type="text"]Matches attribute with value
Descendantdiv spanMatches descendants
Childdiv > spanMatches direct children
Adjacent siblingh1 + pMatches adjacent sibling
General siblingh1 ~ pMatches following siblings
:first-childli:first-childFirst child element
:last-childli:last-childLast child element
:nth-childli:nth-child(2n)Nth child element
:emptydiv:emptyElements with no children
:not()div:not(.hidden)Negation

Structs§

ElementWrapper
Adapter wrapping a DOM node for selector matching.
Filter
Filter criteria for element queries (BeautifulSoup-style).
ScrapeSelector
Marker type for our selector implementation.

Enums§

NonTSPseudoClass
Pseudo-class variants (non-tree-structural).
PseudoElement
Pseudo-element variants (not supported for matching).
QueryError
Error type for query operations.

Functions§

find
Finds the first element matching a CSS selector.
find_all
Finds all elements matching a CSS selector.
find_all_with_selector
Finds all elements matching a pre-parsed selector.
find_all_within
Finds all elements matching a CSS selector within a subtree.
find_all_within_with_selector
Finds all elements matching a selector within a subtree.
find_by_filter
Finds all elements matching a filter.
find_first_by_filter
Finds the first element matching a filter.
find_with_selector
Finds the first element matching a pre-parsed selector.
find_within
Finds the first element matching a CSS selector within a subtree.
find_within_with_selector
Finds the first element matching a selector within a subtree.
matches_selector
Checks if an element matches a selector list.
matches_selector_with_caches
Checks if an element matches a selector list, reusing provided caches.
parse_selector
Parses a CSS selector string into a compiled selector list.

Type Aliases§

QueryResult
Result type alias for query operations.