Crate soup[][src]

Inspired by the Python library “BeautifulSoup,” soup is a layer on top of html5ever that aims to provide a slightly different API for querying & manipulating HTML

Examples (inspired by bs4’s docs)

Here is the HTML document we will be using for the rest of the examples:

const THREE_SISTERS: &'static str = r#"
<html><head><title>The Dormouse's story</title></head>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="" class="sister" id="link1">Elsie</a>,
<a href="" class="sister" id="link2">Lacie</a> and
<a href="" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>

First let’s try searching for a tag with a specific name:

use soup::prelude::*;

let soup = Soup::new(THREE_SISTERS);

let title = soup.tag("title").find().expect("Couldn't find tag 'title'");
assert_eq!(title.display(), "<title>The Dormouse's story</title>");
assert_eq!(, "title");
assert_eq!(title.text(), "The Dormouse's story".to_string());
assert_eq!(title.parent().expect("Couldn't find parent of 'title'").name(), "head");

let p = soup.tag("p").find().expect("Couldn't find tag 'p'");
    r#"<p class="title"><b>The Dormouse's story</b></p>"#
assert_eq!(p.get("class"), Some("title".to_string()));

So we see that .find will give us the first element that matches the query, and we’ve seen some of the methods that we can call on the results. But what if we want to retrieve more than one element with the query? For that, we’ll use .find_all:

// .find returns only the first 'a' tag
let a = soup.tag("a").find().expect("Couldn't find tag 'a'");
    r#"<a class="sister" href="" id="link1">Elsie</a>"#
// but .find_all will return _all_ of them:
let a_s = soup.tag("a").find_all();
assert_eq!(|a| a.display())
    r#"<a class="sister" href="" id="link1">Elsie</a>
<a class="sister" href="" id="link2">Lacie</a>
<a class="sister" href="" id="link3">Tillie</a>"#

Since .find_all returns an iterator, you can use it with all the methods you would use with other iterators:

let expected = [

for (i, link) in soup.tag("a").find_all().enumerate() {
    let href = link.get("href").expect("Couldn't find link with 'href' attribute");
    assert_eq!(href, expected[i].to_string());

The top-level structure we’ve been working with here, soup, implements the same methods that the query results do, so you can call the same methods on it and it will delegate the calls to the root node:

let text = soup.text();
    r#"The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were
Lacie and
and they lived at the bottom of a well.


You can use more than just strings to search for results, such as Regex:

use regex::Regex;

let soup = Soup::new(r#"<body><p>some text, <b>Some bold text</b></p></body>"#);
let results = soup.tag(Regex::new("^b")?)
assert_eq!(results, vec!["body".to_string(), "b".to_string()]);

Passing true will match everything:

let soup = Soup::new(r#"<body><p>some text, <b>Some bold text</b></p></body>"#);
let results = soup.tag(true)
assert_eq!(results, vec![

(also, passing false will always return no results, though if that is useful to you, please let me know)

So what can you do once you get the result of a query? Well, for one thing, you can traverse the tree a few different ways. You can ascend the tree:

let soup = Soup::new(r#"<body><p>some text, <b>Some bold text</b></p></body>"#);
let b = soup.tag("b")
            .expect("Couldn't find tag 'b'");
let p = b.parent()
         .expect("Couldn't find parent of 'b'");
assert_eq!(, "p".to_string());
let body = p.parent()
            .expect("Couldn't find parent of 'p'");
assert_eq!(, "body".to_string());

Or you can descend it:

let soup = Soup::new(r#"<body><ul><li>ONE</li><li>TWO</li><li>THREE</li></ul></body>"#);
let ul = soup.tag("ul")
            .expect("Couldn't find tag 'ul'");
let mut li_tags = ul.children().filter(|child| child.is_element());
assert_eq!(|tag| tag.text().to_string()), Some("ONE".to_string()));
assert_eq!(|tag| tag.text().to_string()), Some("TWO".to_string()));
assert_eq!(|tag| tag.text().to_string()), Some("THREE".to_string()));

Or ascend it with an iterator:

let soup = Soup::new(r#"<body><ul><li>ONE</li><li>TWO</li><li>THREE</li></ul></body>"#);
let li = soup.tag("li").find().expect("Couldn't find tag 'li'");
let mut parents = li.parents();
assert_eq!(|tag|, Some("ul".to_string()));
assert_eq!(|tag|, Some("body".to_string()));
assert_eq!(|tag|, Some("html".to_string()));
assert_eq!(|tag|, Some("[document]".to_string()));



Traits & impls for matching types with strings


This module exports all the important types & traits to use soup effectively



Construct a query to apply to an HTML tree


Parses HTML & provides methods to query & manipulate the document



Adds some convenience methods to the html5ever::rcdom::Node type


Adds the QueryBuilder constructor methods to the implementing type