HTML parsing and querying with CSS selectors.

scraper is on Crates.io and GitHub.

Scraper provides an interface to Servo's html5ever and selectors crates, for browser-grade parsing and querying.

Examples

Parsing a document

``` use htmler::Html;

let html = r#" Hello, world!

Hello, world!

"#;

let document = Html::parse_document(html); ```

Parsing a fragment

use scraper::Html; let fragment = Html::parse_fragment("<h1>Hello, <i>world!</i></h1>");

Parsing a selector

use htmler::Selector; let selector = Selector::new("h1.foo");

Selecting elements

``` use htmler::{Html, Selector};

let html = r#"

"#;

let fragment = Html::parse_fragment(html); let selector = Selector::new("li");

for element in fragment.select(&selector) { assert_eq!("li", element.value().name()); } ```

Selecting descendent elements

``` use htmler::{Html, Selector};

let html = r#"

"#;

let fragment = Html::parsefragment(html); let ulselector = Selector::new("ul"); let li_selector = Selector::new("li");

let ul = fragment.select(&ulselector).next().unwrap(); for element in ul.select(&liselector) { assert_eq!("li", element.value().name()); } ```

Accessing element attributes

``` use htmler::{Html, Selector};

let fragment = Html::parse_fragment(r#""#); let selector = Selector::new(r#"input[name="foo"]"#);

let input = fragment.select(&selector).next().unwrap(); assert_eq!(Some("bar"), input.value().attr("value")); ```

Serializing HTML and inner HTML

``` use htmler::{Html, Selector};

let fragment = Html::parse_fragment("

Hello, world!

"); let selector = Selector::new("h1");

let h1 = fragment.select(&selector).next().unwrap();

asserteq!("

Hello, world!

", h1.html()); asserteq!("Hello, world!", h1.inner_html()); ```

Accessing descendent text

``` use htmler::{Html, Selector};

let fragment = Html::parse_fragment("

Hello, world!

"); let selector = Selector::new("h1");

let h1 = fragment.select(&selector).next().unwrap(); let text = h1.text().collect::>();

assert_eq!(vec!["Hello, ", "world!"], text); ```