Bill Chung
Bill Chung

Reputation: 145

Parse html string to json using htmlparser2

I'm trying to use htmlparser2 (https://www.npmjs.com/package/htmlparser2) to parse a html raw string into json.

But its usage only logs out the tag/text, what I want is the json like its livedemo (http://demos.forbeslindesay.co.uk/htmlparser2/), so that I can filter the elements I want.

Any help or suggestions would be appreciated!

Upvotes: 5

Views: 3440

Answers (1)

Bill Chung
Bill Chung

Reputation: 145

took a look of the source codes, it seems there is a helper method parseDOM that can do the trick:

var htmlparser = require("htmlparser2");
elements = htmlparser.parseDOM(htmlString);

so the elements will be the array of objects like the livedemo.

hope this will help anyone encounters the same problem.

Upvotes: 6

Related Questions