#dom #pest-parser #pest #html

html_parser

A simple and general purpose html/xhtml parser

13 unstable releases

0.7.0 May 11, 2023
0.6.3 Apr 21, 2022
0.6.2 Dec 21, 2020
0.5.0 Nov 28, 2020
0.1.0 May 20, 2020

#1341 in Web programming

Download history 17671/week @ 2025-10-06 18788/week @ 2025-10-13 18827/week @ 2025-10-20 20269/week @ 2025-10-27 20338/week @ 2025-11-03 17346/week @ 2025-11-10 17731/week @ 2025-11-17 17924/week @ 2025-11-24 20007/week @ 2025-12-01 20134/week @ 2025-12-08 21213/week @ 2025-12-15 15514/week @ 2025-12-22 14196/week @ 2025-12-29 19640/week @ 2026-01-05 20990/week @ 2026-01-12 23776/week @ 2026-01-19

80,516 downloads per month
Used in 86 crates (28 directly)

MIT license

55KB
591 lines

Html parser

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

Features

  • 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">
                <head>
                    <meta charset="utf-8">
                    <title>Html parser</title>
                </head>
                <body>
                    <h1 id="a" class="b c">Hello world</h1>
                    </h1> <!-- comments & dangling elements are ignored -->
                </body>
            </html>"#;

        assert!(Dom::parse(html).is_ok());
    }

Parse html fragment

    use html_parser::Dom;

    fn main() {
        let html = "<div id=cat />";
        assert!(Dom::parse(html).is_ok());
    }

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);
        Ok(())
    }

Dependencies

~2.3–3.5MB
~70K SLoC