Expand description


Html parser

A simple and general purpose html/xhtml parser lib/bin, using Pest.


  • Parse html & xhtml (not xml processing instructions)
  • Parse html-documents
  • Parse html-fragments
  • Parse empty documents
  • Parse with the same api for both documents and fragments
  • Parse custom, non-standard, elements; <cat/>, <Cat/> and <C4-t/>
  • Removes comments
  • Removes dangling elements
  • Iterate over all nodes in the dom three

What is it not

  • It’s not a high-performance browser-grade parser
  • It’s not suitable for html validation
  • It’s not a parser that includes element selection or dom manipulation

If your requirements matches any of the above, then you’re most likely looking for one of the crates below:

Examples bin

Parse html file

html_parser index.html

Parse stdin with pretty output

curl <website> | html_parser -p

Examples lib

Parse html document

    use html_parser::Dom;

    fn main() {
        let html = r#"
            <!doctype html>
            <html lang="en">
                    <meta charset="utf-8">
                    <title>Html parser</title>
                    <h1 id="a" class="b c">Hello world</h1>
                    </h1> <!-- comments & dangling elements are ignored -->


Parse html fragment

    use html_parser::Dom;

    fn main() {
        let html = "<div id=cat />";

Print to json

    use html_parser::{Dom, Result};

    fn main() -> Result<()> {
        let html = "<div id=cat />";
        let json = Dom::parse(html)?.to_json_pretty()?;
        println!("{}", json);


The main struct & the result of the parsed html

Most of the parsed html nodes are elements, except for text


Document, DocumentFragment or Empty

Normal: <div></div> or Void: <meta/>and <meta>

Type Definitions