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#"
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie ,
Lacie and
Tillie ;
and they lived at the bottom of a well.
...
"#;
# fn main() {}
First let's try searching for a tag with a specific name:
# extern crate soup;
# const THREE_SISTERS: &'static str = r#"
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie ,
# Lacie and
# Tillie ;
# and they lived at the bottom of a well.
#
# ...
# "#;
# fn main() {
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(), " The Dormouse's story ");
assert_eq!(title.name(), "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'");
assert_eq!(
p.display(),
r#" The Dormouse's story "#
);
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
:
# extern crate soup;
# use soup::prelude::*;
# const THREE_SISTERS: &'static str = r#"
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie ,
# Lacie and
# Tillie ;
# and they lived at the bottom of a well.
#
# ...
# "#;
# fn main() {
# let soup = Soup::new(THREE_SISTERS);
// .find returns only the first 'a' tag
let a = soup.tag("a").find().expect("Couldn't find tag 'a'");
assert_eq!(
a.display(),
r#" Elsie "#
);
// but .find_all will return _all_ of them:
let a_s = soup.tag("a").find_all();
assert_eq!(
a_s.map(|a| a.display())
.collect:: >()
.join("\n"),
r#" Elsie
Lacie
Tillie "#
);
# }
Since .find_all
returns an iterator, you can use it with all the methods you would
use with other iterators:
# extern crate soup;
# use soup::prelude::*;
# const THREE_SISTERS: &'static str = r#"
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie ,
# Lacie and
# Tillie ;
# and they lived at the bottom of a well.
#
# ...
# "#;
# fn main() {
# let soup = Soup::new(THREE_SISTERS);
let expected = [
"http://example.com/elsie",
"http://example.com/lacie",
"http://example.com/tillie",
];
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:
# extern crate soup;
# use soup::prelude::*;
# const THREE_SISTERS: &'static str = r#"
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie ,
# Lacie and
# Tillie ;
# and they lived at the bottom of a well.
#
# ...
# "#;
# fn main() {
# let soup = Soup::new(THREE_SISTERS);
let text = soup.text();
assert_eq!(
text,
r#"The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
"#
);
# }
You can use more than just strings to search for results, such as Regex:
# extern crate regex;
# extern crate soup;
# use *;
# use Error;
use Regex;
#
Passing true
will match everything:
# extern crate soup;
# use *;
# use Error;
#
(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:
# extern crate soup;
# use *;
# use Error;
#
Or you can descend it:
# extern crate soup;
# use *;
# use Error;
#
Or ascend it with an iterator:
# extern crate soup;
# use *;
# use Error;
#