Struct soup::QueryBuilder

source ·
pub struct QueryBuilder { /* 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§

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

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().unwrap();
assert_eq!(result.get("id"), Some("bold-tag".to_string()));

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().unwrap();
assert_eq!(result.name(), "b".to_string());

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().unwrap();
assert_eq!(result.name(), "section".to_string());

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().unwrap();
assert_eq!(result.get("id"), Some("one".to_string()));

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.