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
selectorscrate - 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
| Selector | Example | Description |
|---|---|---|
| Type | div | Matches elements by tag name |
| Class | .foo | Matches elements with class |
| ID | #bar | Matches element by ID |
| Universal | * | Matches all elements |
| Attribute | [href] | Matches elements with attribute |
| Attribute value | [type="text"] | Matches attribute with value |
| Descendant | div span | Matches descendants |
| Child | div > span | Matches direct children |
| Adjacent sibling | h1 + p | Matches adjacent sibling |
| General sibling | h1 ~ p | Matches following siblings |
| :first-child | li:first-child | First child element |
| :last-child | li:last-child | Last child element |
| :nth-child | li:nth-child(2n) | Nth child element |
| :empty | div:empty | Elements with no children |
| :not() | div:not(.hidden) | Negation |
Structs§
- Element
Wrapper - Adapter wrapping a DOM node for selector matching.
- Filter
- Filter criteria for element queries (BeautifulSoup-style).
- Scrape
Selector - Marker type for our selector implementation.
Enums§
- NonTS
Pseudo Class - Pseudo-class variants (non-tree-structural).
- Pseudo
Element - Pseudo-element variants (not supported for matching).
- Query
Error - 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§
- Query
Result - Result type alias for query operations.