pub struct QueryBuilder<'a, T: Query + 'a = (), U: Query + 'a = ()> { /* private fields */ }Expand description
Construct a query to apply to an HTML tree
§Example
let soup = Soup::new(r#"<div id="foo">BAR</div><div id="baz">QUUX</div>"#);
let query = soup.tag("div") // result must be a div
.attr("id", "foo") // with id "foo"
.find(); // executes the query, returns the first resultImplementations§
Source§impl<'a, T, U> QueryBuilder<'a, T, U>where
T: Query + 'a,
U: Query + 'a,
impl<'a, T, U> QueryBuilder<'a, T, U>where
T: Query + 'a,
U: Query + 'a,
Sourcepub fn limit(self, limit: usize) -> QueryBuilder<'a, T, U>
pub fn limit(self, limit: usize) -> QueryBuilder<'a, T, U>
Adds a limit to the number of results that can be returned
This method adds an upper bound to the number of results that will be returned by the query
§Example
let soup = Soup::new(r#"<div id="one"></div><div id="two"></div><div id="three></div>"#);
let results = soup.tag("div").limit(2).find_all().collect::<Vec<_>>();
assert_eq!(results.len(), 2);Sourcepub fn tag<P: Pattern>(
self,
tag: P,
) -> QueryBuilder<'a, TagQuery<P>, QueryWrapper<'a, T, U>>
pub fn tag<P: Pattern>( self, tag: P, ) -> QueryBuilder<'a, TagQuery<P>, QueryWrapper<'a, T, U>>
Specifies a tag for which to search
§Example
let soup =
Soup::new(r#"<div>Test</div><section><b id="bold-tag">SOME BOLD TEXT</b></section>"#);
let result = soup.tag("b").find().expect("Couldn't find tag 'b'");
assert_eq!(result.get("id"), Some("bold-tag".to_string()));Sourcepub fn attr_name<P>(
self,
name: P,
) -> QueryBuilder<'a, AttrQuery<P, bool>, QueryWrapper<'a, T, U>>where
P: Pattern,
pub fn attr_name<P>(
self,
name: P,
) -> QueryBuilder<'a, AttrQuery<P, bool>, QueryWrapper<'a, T, U>>where
P: Pattern,
Searches for a tag that has an attribute with the specified name
§Example
let soup = Soup::new(r#"<div>Test</div><section><b id="bold-tag">SOME BOLD TEXT</b></section>"#);
let result = soup.attr_name("id").find().expect("Couldn't find element with an 'id'");
assert_eq!(result.name(), "b");Sourcepub fn attr_value<P>(
self,
value: P,
) -> QueryBuilder<'a, AttrQuery<bool, P>, QueryWrapper<'a, T, U>>where
P: Pattern,
pub fn attr_value<P>(
self,
value: P,
) -> QueryBuilder<'a, AttrQuery<bool, P>, QueryWrapper<'a, T, U>>where
P: Pattern,
Search for a node with any attribute with a value that matches the specified value
§Example
let soup = Soup::new(r#"<div>Test</div><section><b id="bold-tag">SOME BOLD TEXT</b></section>"#);
let result = soup.attr_value("bold-tag").find().expect("Couldn't find a tag with attribute value 'bold-tag'");
assert_eq!(result.name(), "b");Sourcepub fn attr<P, Q>(
self,
name: P,
value: Q,
) -> QueryBuilder<'a, AttrQuery<P, Q>, QueryWrapper<'a, T, U>>
pub fn attr<P, Q>( self, name: P, value: Q, ) -> QueryBuilder<'a, AttrQuery<P, Q>, QueryWrapper<'a, T, U>>
Specifies an attribute name/value pair for which to search
§Example
let soup =
Soup::new(r#"<div>Test</div><section><b id="bold-tag">SOME BOLD TEXT</b></section>"#);
let result = soup.attr("id", "bold-tag").find().expect("Couldn't find tag with id 'bold-tag'");
assert_eq!(result.name(), "b".to_string());Sourcepub fn class<P: Pattern>(
self,
value: P,
) -> QueryBuilder<'a, AttrQuery<&'static str, P>, QueryWrapper<'a, T, U>>
pub fn class<P: Pattern>( self, value: P, ) -> QueryBuilder<'a, AttrQuery<&'static str, P>, QueryWrapper<'a, T, U>>
Specifies a class name for which to search
§Example
let soup = Soup::new(
r#"<div>Test</div><section class="content"><b id="bold-tag">SOME BOLD TEXT</b></section>"#,
);
let result = soup.class("content").find().expect("Couldn't find tag with class 'content'");
assert_eq!(result.name(), "section".to_string());Sourcepub fn recursive(self, recursive: bool) -> Self
pub fn recursive(self, recursive: bool) -> Self
Specifies whether the query should recurse all the way through the document, or stay localized to the queried tag and it’s children
Sourcepub fn find(self) -> Option<Handle>
pub fn find(self) -> Option<Handle>
Executes the query, and returns either the first result, or None
§Example
let soup = Soup::new(
r#"<ul><li id="one">One</li><li id="two">Two</li><li id="three">Three</li></ul>"#,
);
let result = soup.tag("li").find().expect("Couldn't find 'li'");
assert_eq!(result.get("id"), Some("one".to_string()));Sourcepub fn find_all(self) -> Box<dyn Iterator<Item = Handle> + 'a>
pub fn find_all(self) -> Box<dyn Iterator<Item = Handle> + 'a>
Executes the query, and returns an iterator of the results
§Example
let soup = Soup::new(
r#"<ul><li id="one">One</li><li id="two">Two</li><li id="three">Three</li></ul>"#,
);
let results = soup.tag("li").find_all().collect::<Vec<_>>();
assert_eq!(results.len(), 3);
assert_eq!(results[0].display(), "<li id=\"one\">One</li>");
assert_eq!(results[1].display(), "<li id=\"two\">Two</li>");
assert_eq!(results[2].display(), "<li id=\"three\">Three</li>");Trait Implementations§
Source§impl<'a, T: Query + 'a, U: Query + 'a> Debug for QueryBuilder<'a, T, U>
impl<'a, T: Query + 'a, U: Query + 'a> Debug for QueryBuilder<'a, T, U>
Source§impl<'a, T: Query + 'a, U: Query + 'a> IntoIterator for QueryBuilder<'a, T, U>
impl<'a, T: Query + 'a, U: Query + 'a> IntoIterator for QueryBuilder<'a, T, U>
Auto Trait Implementations§
impl<'a, T, U> Freeze for QueryBuilder<'a, T, U>
impl<'a, T = (), U = ()> !RefUnwindSafe for QueryBuilder<'a, T, U>
impl<'a, T = (), U = ()> !Send for QueryBuilder<'a, T, U>
impl<'a, T = (), U = ()> !Sync for QueryBuilder<'a, T, U>
impl<'a, T, U> Unpin for QueryBuilder<'a, T, U>
impl<'a, T = (), U = ()> !UnwindSafe for QueryBuilder<'a, T, U>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more