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§
source§impl QueryBuilder
impl QueryBuilder
sourcepub fn limit(&mut self, limit: usize) -> &mut QueryBuilder
pub fn limit(&mut self, limit: usize) -> &mut QueryBuilder
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(&mut self, tag: &str) -> &mut QueryBuilder
pub fn tag(&mut self, tag: &str) -> &mut QueryBuilder
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()));
sourcepub fn attr(&mut self, name: &str, value: &str) -> &mut QueryBuilder
pub fn attr(&mut self, name: &str, value: &str) -> &mut QueryBuilder
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());
sourcepub fn class(&mut self, value: &str) -> &mut QueryBuilder
pub fn class(&mut self, value: &str) -> &mut QueryBuilder
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());
sourcepub fn find(&mut self) -> Option<Handle>
pub fn find(&mut 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().unwrap();
assert_eq!(result.get("id"), Some("one".to_string()));
sourcepub fn find_all(&self) -> Box<dyn Iterator<Item = Handle>>
pub fn find_all(&self) -> Box<dyn Iterator<Item = Handle>>
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 Clone for QueryBuilder
impl Clone for QueryBuilder
source§fn clone(&self) -> QueryBuilder
fn clone(&self) -> QueryBuilder
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more