# accessibility-scraper [![crates.io](https://img.shields.io/crates/v/accessibility-scraper?color=dark-green)][crate] [![downloads](https://img.shields.io/crates/d/accessibility-scraper)][crate] [![test](https://github.com/causal-agent/accessibility-scraper/actions/workflows/test.yml/badge.svg)][tests] HTML parsing and querying with CSS selectors. `accessibility-scraper` is on [Crates.io][crate] and [GitHub][github]. [crate]: https://crates.io/crates/accessibility-scraper [github]: https://github.com/causal-agent/accessibility-scraper [tests]: https://github.com/causal-agent/accessibility-scraper/actions/workflows/test.yml Scraper provides an interface to Servo's `fast_html5ever` and `selectors` crates, for browser-grade parsing and querying. ## Examples ### Parsing a document ```rust use accessibility-scraper::Html; let html = r#" Hello, world!

Hello, world!

"#; let document = Html::parse_document(html); ``` ### Parsing a fragment ```rust use accessibility-scraper::Html; let fragment = Html::parse_fragment("

Hello, world!

"); ``` ### Parsing a selector ```rust use accessibility-scraper::Selector; let selector = Selector::parse("h1.foo").unwrap(); ``` ### Selecting elements ```rust use accessibility-scraper::{Html, Selector}; let html = r#" "#; let fragment = Html::parse_fragment(html); let selector = Selector::parse("li").unwrap(); for element in fragment.select(&selector) { assert_eq!("li", element.value().name()); } ``` ### Selecting descendent elements ```rust use accessibility-scraper::{Html, Selector}; let html = r#" "#; let fragment = Html::parse_fragment(html); let ul_selector = Selector::parse("ul").unwrap(); let li_selector = Selector::parse("li").unwrap(); let ul = fragment.select(&ul_selector).next().unwrap(); for element in ul.select(&li_selector) { assert_eq!("li", element.value().name()); } ``` ### Accessing element attributes ```rust use accessibility-scraper::{Html, Selector}; let fragment = Html::parse_fragment(r#""#); let selector = Selector::parse(r#"input[name="foo"]"#).unwrap(); let input = fragment.select(&selector).next().unwrap(); assert_eq!(Some("bar"), input.value().attr("value")); ``` ### Serializing HTML and inner HTML ```rust use accessibility-scraper::{Html, Selector}; let fragment = Html::parse_fragment("

Hello, world!

"); let selector = Selector::parse("h1").unwrap(); let h1 = fragment.select(&selector).next().unwrap(); assert_eq!("

Hello, world!

", h1.html()); assert_eq!("Hello, world!", h1.inner_html()); ``` ### Accessing descendent text ```rust use accessibility-scraper::{Html, Selector}; let fragment = Html::parse_fragment("

Hello, world!

"); let selector = Selector::parse("h1").unwrap(); let h1 = fragment.select(&selector).next().unwrap(); let text = h1.text().collect::>(); assert_eq!(vec!["Hello, ", "world!"], text); ``` ### Manipulating the DOM ```rust use fast_html5ever::tree_builder::TreeSink; use accessibility-scraper::{Html, Selector}; let html = "hello

REMOVE ME

"; let selector = Selector::parse(".hello").unwrap(); let mut document = Html::parse_document(html); let node_ids: Vec<_> = document.select(&selector).map(|x| x.id()).collect(); for id in node_ids { document.remove_from_parent(&id); } assert_eq!(document.html(), "hello"); ``` ## Contributing Please feel free to open pull requests. If you're planning on implementing something big (i.e. not fixing a typo, a small bug fix, minor refactor, etc) then please open an issue first.