EN
Node.js - parse html to DOM with htmlparser2 library
7 points
In this short article, we would like to show how in Node.js parse HTML to Simple DOM - it is not real DOM but is enough in many cases giving good parser performance (check performance section).
Note: the main advantage of this library is portability (Works in any JavaScript - even with React and SSR)
xxxxxxxxxx
1
npm install --save htmlparser2
index.js
file:
xxxxxxxxxx
1
const htmlparser2 = require("htmlparser2");
2
3
const handler = new htmlparser2.DomHandler();
4
const parser = new htmlparser2.Parser(handler);
5
6
parser.write('<div><p>1. Some text .../p><p>2. Some text .../p></div>');
7
parser.end();
8
9
const root = handler.root;
10
11
console.log(root);
Screenshot:

index.ts
file:
xxxxxxxxxx
1
import { DomHandler, Parser } from 'htmlparser2';
2
3
const handler = new DomHandler();
4
const parser = new Parser(handler);
5
6
parser.write('<div><p>1. Some text .../p><p>2. Some text .../p></div>');
7
parser.end();
8
9
const root = handler.root;
10
11
console.log(root);