@broadly/htmlparser2

中文文档
3.9.0发布于 2015/08/20MITCommonJS近 30 天下载 1万

Fast & forgiving HTML/XML/RSS parser

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Documentation
  4. Get a DOM
  5. Parsing RSS/RDF/Atom Feeds
  6. Performance
  7. How does this module differ from node-htmlparser ?

htmlparser2

NPM version Downloads Build Status Coverage

A forgiving HTML/XML/RSS parser. The parser can handle streams and provides a callback interface.

Installation

npm install htmlparser2

A live demo of htmlparser2 is available here.

Usage

var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
    onopentag: function(name, attribs){
        if(name === "script" && attribs.type === "text/javascript"){
            console.log("JS! Hooray!");
        }
    },
    ontext: function(text){
        console.log("-->", text);
    },
    onclosetag: function(tagname){
        if(tagname === "script"){
            console.log("That's it?!");
        }
    }
}, {decodeEntities: true});
parser.write("Xyz <script type='text/javascript'>var foo = '<<bar>>';</ script>");
parser.end();

Output (simplified):

--> Xyz 
JS! Hooray!
--> var foo = '<<bar>>';
That's it?!

Documentation

Read more about the parser and its options in the wiki.

Get a DOM

The DomHandler (known as DefaultHandler in the original htmlparser module) produces a DOM (document object model) that can be manipulated using the DomUtils helper.

The DomHandler, while still bundled with this module, was moved to its own module. Have a look at it for further information.

Parsing RSS/RDF/Atom Feeds

new htmlparser.FeedHandler(function(<error> error, <object> feed){
    ...
});

Note: While the provided feed handler works for most feeds, you might want to use danmactough/node-feedparser, which is much better tested and actively maintained.

Performance

After having some artificial benchmarks for some time, @AndreasMadsen published his htmlparser-benchmark, which benchmarks HTML parses based on real-world websites.

At the time of writing, the latest versions of all supported parsers show the following performance characteristics on Travis CI (please note that Travis doesn't guarantee equal conditions for all tests):

gumbo-parser   : 34.9208 ms/file ± 21.4238
html-parser    : 24.8224 ms/file ± 15.8703
html5          : 419.597 ms/file ± 264.265
htmlparser     : 60.0722 ms/file ± 384.844
htmlparser2-dom: 12.0749 ms/file ± 6.49474
htmlparser2    : 7.49130 ms/file ± 5.74368
hubbub         : 30.4980 ms/file ± 16.4682
libxmljs       : 14.1338 ms/file ± 18.6541
parse5         : 22.0439 ms/file ± 15.3743
sax            : 49.6513 ms/file ± 26.6032

How does this module differ from node-htmlparser?

This is a fork of the htmlparser module. The main difference is that this is intended to be used only with node (it runs on other platforms using browserify). htmlparser2 was rewritten multiple times and, while it maintains an API that's compatible with htmlparser in most cases, the projects don't share any code anymore.

The parser now provides a callback interface close to sax.js (originally targeted at readabilitySAX). As a result, old handlers won't work anymore.

The DefaultHandler and the RssHandler were renamed to clarify their purpose (to DomHandler and FeedHandler). The old names are still available when requiring htmlparser2, your code should work as expected.

版本与发布记录

@broadly/htmlparser2 共发布 1 个版本,首次发布于 2015/08/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.9.0latest2015/08/2011 年前

关于 @broadly/htmlparser2 的常见问题

@broadly/htmlparser2 是什么?

Fast & forgiving HTML/XML/RSS parser注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @broadly/htmlparser2?

在项目目录运行 npm install @broadly/htmlparser2,或使用 pnpm add / yarn add / bun add。

@broadly/htmlparser2 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/broadly__htmlparser2 是否存在。

@broadly/htmlparser2 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@broadly/htmlparser2 可以商用吗?

@broadly/htmlparser2 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@broadly/htmlparser2 的下载量有多少?

根据 npm 官方下载统计,@broadly/htmlparser2 在 2026/10/09 – 2026/11/07 这 30 天内被下载 10,237 次,日均约 341 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@broadly/htmlparser2 还在维护吗?

最新版本 3.9.0 发布于 2015/08/20(11 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2015/08/20。作者已将其标记为弃用,新项目建议寻找替代方案。