5

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!

2
  • I tried to get returns from paser.write(html), but it returns nothing, and I also tried to return data from ondataend, still no luck. Commented Aug 1, 2015 at 8:40
  • Showing the code of what you tried is better than describing it and more helpful to anyone trying to help. Commented Aug 8, 2015 at 15:06

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.