Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 212
Bill Chung Avatar asked Aug 01 '15 08:08

Bill Chung


1 Answers

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.

like image 99
Bill Chung Avatar answered Oct 20 '22 11:10

Bill Chung