Languages
[Edit]
EN

Node.js - parse html to DOM with htmlparser2 library

7 points
Created by:
Root-ssh
115270

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)

Librabry installation

npm install --save htmlparser2

Usage example

index.js file: 

const htmlparser2 = require("htmlparser2");

const handler = new htmlparser2.DomHandler();
const parser = new htmlparser2.Parser(handler);

parser.write('<div><p>1. Some text .../p><p>2. Some text .../p></div>');
parser.end();

const root = handler.root;

console.log(root);

Screenshot:

Node.js HTML parsing with htmlparser2 library.
Node.js HTML parsing with htmlparser2 library.

TypeScript version

index.ts file:

import { DomHandler, Parser } from 'htmlparser2';

const handler = new DomHandler();
const parser = new Parser(handler);

parser.write('<div><p>1. Some text .../p><p>2. Some text .../p></div>');
parser.end();

const root = handler.root;

console.log(root);

References

  1.  https://www.npmjs.com/package/htmlparser2
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join