Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
HTML parsing and querying with CSS selectors.
scraper is a Rust crate that parses HTML documents and fragments and queries them with CSS selectors, built directly on Servo's html5ever and selectors crates for browser-grade parsing.
scraper is a Rust library published on crates.io (current version 0.27.0) and hosted on GitHub under the rust-scraper organization. It takes an HTML string as input and produces a queryable DOM tree via the Html::parse_document or Html::parse_fragment methods. You then use CSS selectors to select elements, read attributes, extract text, serialize HTML, and even manipulate the DOM through the HtmlTreeSink trait.
html5ever parser, so it handles real-world HTML with error recovery exactly like a browser would.selectors crate to support selecting elements with CSS selectors such as h1.foo, ul li, or input[name="foo"].Html::parse_document parses a full document; Html::parse_fragment parses a snippet without requiring html/head/body structure.attr("value"), collect descendant text with text(), and serialize elements to HTML with html() or inner_html().HtmlTreeSink and the TreeSink trait, you can remove nodes from the tree and finish back into a Html document.atomic feature switches the internal Tendril string to atomic reference counting, making the types Send and usable across threads.scraper = "0.27.0" or with the atomic feature enabled.Html document and use CSS selectors to pull out specific elements, attributes, and text.HtmlTreeSink, and serialize the modified tree back to HTML.Use Html::parse_document or Html::parse_fragment to parse an HTML string into a traversable tree. Parse a CSS selector with Selector::parse, then call select on the tree to iterate over matching elements. For cross-thread usage, enable the atomic feature to change the internal string type to an atomic reference-counted Tendril.
Add scraper = "0.27.0" to your Cargo.toml dependencies. For cross-thread usage, enable the atomic feature: scraper = { version = "0.27.0", features = ["atomic"] }.
parse_document expects a full HTML document (including doctype, html, head, and body elements) and returns a Html tree that mirrors the complete document structure. parse_fragment parses an HTML snippet without those document-level tags and returns a tree representing just the fragment's elements.
By default, the internal Tendril string type is thread-local and not Send. Enabling the atomic feature switches to atomic reference counting, which implements Send and allows the types to be used across threads.
