Skip to content

htmlparser2vsparse5

MIT 12 4 4,789
277.1 million (month) Aug 28 2011 12.0.0(2026-03-20 23:08:40 ago)
3,886 7 34 MIT
Jul 03 2013 419.4 million (month) 8.0.0(2026-02-21 19:30:52 ago)

htmlparser2 is a Node.js library for parsing HTML and XML documents. It works by building a tree of elements, similar to the Document Object Model (DOM) in web browsers. This allows you to easily traverse and manipulate the structure of the document.

htmlparser2 is a low-level html tree parser but it can still be useful in web scraping as it's a powerful tool for HTML restructuring and serialization.

parse5 is a Node.js library for parsing and manipulating HTML and XML documents. It is designed to be fast and flexible, and it is commonly used in web scraping and web development projects.

parse5 is used by popular libraries such as Angular, Lit, Cheerio and many more. Unlike Cheerio parse5 is a low level html parsing library that might be useful directly in web scraping without higher level abstraction.

Example Use


```javascript const htmlparser = require("htmlparser2"); const parser = new htmlparser.Parser({ onopentag: (name, attribs) => { console.log(`Opening tag: ${name}`); }, ontext: (text) => { console.log(`Text: ${text}`); }, onclosetag: (name) => { console.log(`Closing tag: ${name}`); } }, {decodeEntities: true}); const html = "

Hello, world!

"; parser.write(html); parser.end(); ```
```javascript const parse5 = require("parse5"); // parse string const document = parse5.parse('Hello World!'); console.log(document); // html tree can be traversed as javascript object: const body = document.childNodes[1]; console.log(body.childNodes[0].value); // "Hello World!" // and modified const newElement = parse5.parseFragment('

New Element

'); body.appendChild(newElement.childNodes[0]); console.log(parse5.serialize(document)); ```

Alternatives / Similar


Was this page helpful?