使用 htmlparser2 将 html 字符串解析为 json

Parse html string to json using htmlparser2

我正在尝试使用 htmlparser2 (https://www.npmjs.com/package/htmlparser2) 将 html 原始字符串解析为 json。

但是它的用法只是注销了tag/text,我想要的是像它的livedemo(http://demos.forbeslindesay.co.uk/htmlparser2/)那样的json,这样我就可以过滤我想要的元素。

如有任何帮助或建议,我们将不胜感激!

查看了源代码,似乎有一个辅助方法 parseDOM 可以解决这个问题:

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

所以 元素 将是像 livedemo 一样的对象数组。

希望这对遇到同样问题的人有所帮助。