Parsing HTML in Rust: Libraries, Tools, and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Parsing HTML in Rust: Library Overview and Practical Examples



Rust is a systems programming language gaining popularity in web scraping and HTML parsing tasks due to its performance, memory safety, and strong typing. In this article, we will review the main libraries for parsing HTML in Rust, their capabilities, and provide code examples for each.



1. The scraper Library



scraper is one of the most popular and convenient libraries for parsing HTML in Rust. It is built on top of html5ever (an HTML5 parser) and provides a high-level API for navigating the DOM tree using CSS selectors.



Installation

[dependencies]scraper = "0.19"


Example: Getting Article Headings

use scraper::{Html, Selector};

fn main() { let html = r#" <html> <body> <h1 class="title">First Article</h1> <h1 class="title">Second Article</h1> </body> </html> "#;

let document = Html::parse_document(html); let selector = Selector::parse("h1.title").unwrap();

for element in document.select(&selector) { println!("{}", element.text().collect::<Vec<_>>().join(" ")); }}


Advantages:

  • Intuitive API for CSS selectors
  • Support for all standard pseudo-classes and combinators
  • High performance
  • Good documentation


2. The select.rs Library



select.rs is another powerful library for parsing HTML, based on html5ever. It offers a jQuery-like API, making it especially convenient for developers familiar with JavaScript.



Installation

[dependencies]select = "0.6"


Example: Extracting Links

use select::document::Document;use select::predicate::{Name, Class};

fn main() { let html = r#" <html> <body> <a href="https://example.com" class="link">Example</a> <a href="https://rust-lang.org" class="link">Rust</a> </body> </html> "#;

let document = Document::from(html);

for node in document.find(Class("link")) { if let Some(href) = node.attr("href") { println!("Link: {}", href); } }}


Advantages:

  • jQuery-like syntax
  • Flexible predicates for element searching
  • Support for attributes and text content


3. The html5ever Library



html5ever is a low-level HTML5 parser written in Rust. It serves as the foundation for many other libraries (including scraper and select.rs). If you need full control over the parsing process, this is an excellent choice.



Installation

[dependencies]html5ever = "0.27"


Example: Tokenizing HTML

use html5ever::tendril::TendrilSink;use html5ever::parse_document;use html5ever::rcdom::{RcDom, Handle, NodeData};

fn main() { let html = "<p>Hello, Rust!</p>"; let dom = parse_document(RcDom::default(), Default::default()) .from_utf8() .read_from(&mut html.as_bytes()) .unwrap();

// Traversing the DOM tree fn walk(node: &Handle, depth: usize) { match node.data { NodeData::Text { ref contents } => { let text = contents.borrow(); println!("{:indent$}Text: {}", "", text, indent = depth * 2); } NodeData::Element { ref name,

Recommendations