QueryBuilder

Struct QueryBuilder 

Source
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 result

Implementations§

Source§

impl<'a, T, U> QueryBuilder<'a, T, U>
where T: Query + 'a, U: Query + 'a,

Source

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);
Source

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()));
Source

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");
Source

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");
Source

pub fn attr<P, Q>( self, name: P, value: Q, ) -> QueryBuilder<'a, AttrQuery<P, Q>, QueryWrapper<'a, T, U>>
where P: Pattern, Q: Pattern,

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());
Source

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());
Source

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

Source

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()));
Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: Query + 'a, U: Query + 'a> IntoIterator for QueryBuilder<'a, T, U>

Source§

type IntoIter = Box<dyn Iterator<Item = NodeRef> + 'a>

Which kind of iterator are we turning this into?
Source§

type Item = NodeRef

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a, T, U> Freeze for QueryBuilder<'a, T, U>
where T: Freeze, U: Freeze,

§

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>
where T: Unpin, U: Unpin,

§

impl<'a, T = (), U = ()> !UnwindSafe for QueryBuilder<'a, T, U>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.