Crate soup

source ·
Expand description

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

let html = r#"
<!DOCTYPE html>
<html>
  <head>
    <title>My title</title>
  </head>
  <body>
    <h1>My Heading</h1>
    <p>Some text</p>
    <p>Some more text</p>
  </body>
</html>
"#;

let soup = Soup::new(html);

assert_eq!(
    soup.find()
        .tag("p")
        .execute()?
        .and_then(|p| p.text()),
    Some("Some text".to_string())
);

assert_eq!(
    soup.find_all()
        .tag("p")
        .execute()?
        .iter()
        .map(|p| p.text())
        .collect::<Vec<_>>(),
    vec![Some("Some text".to_string()), Some("Some more text".to_string())]
);

Modules

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

Structs

Parses HTML & provides methods to query & manipulate the document

Traits

Builds a query that returns the first element that matches
Builds a query that returns all matching elements
Adds some convenience methods to the html5ever::rcdom::Node type