Search code examples
javascriptjsonhtml-parsing

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!


Solution

  • 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.