{"id":13457765,"url":"https://github.com/fb55/htmlparser2","last_synced_at":"2025-05-13T10:53:15.370Z","repository":{"id":37445088,"uuid":"2279062","full_name":"fb55/htmlparser2","owner":"fb55","description":"The fast \u0026 forgiving HTML and XML parser","archived":false,"fork":false,"pushed_at":"2025-05-12T12:23:50.000Z","size":6300,"stargazers_count":4573,"open_issues_count":22,"forks_count":390,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-05-13T02:39:07.633Z","etag":null,"topics":["dom","html","html-parser","htmlparser2","javascript","parser","xml"],"latest_commit_sha":null,"homepage":"https://feedic.com/htmlparser2","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fb55.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["fb55"],"tidelift":"npm/htmlparser2"}},"created_at":"2011-08-27T12:25:15.000Z","updated_at":"2025-05-12T14:12:20.000Z","dependencies_parsed_at":"2024-01-08T14:24:12.345Z","dependency_job_id":"80351ddd-9e5d-4059-adf3-a1204b4cad35","html_url":"https://github.com/fb55/htmlparser2","commit_stats":{"total_commits":2025,"total_committers":72,"mean_commits":28.125,"dds":0.5446913580246914,"last_synced_commit":"a3604c5510bf99b1e593c74fd632c6db5a749bf3"},"previous_names":[],"tags_count":66,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fb55%2Fhtmlparser2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fb55%2Fhtmlparser2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fb55%2Fhtmlparser2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fb55%2Fhtmlparser2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fb55","download_url":"https://codeload.github.com/fb55/htmlparser2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253929230,"owners_count":21985799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dom","html","html-parser","htmlparser2","javascript","parser","xml"],"created_at":"2024-07-31T09:00:36.072Z","updated_at":"2025-05-13T10:53:15.323Z","avatar_url":"https://github.com/fb55.png","language":"TypeScript","readme":"# htmlparser2\n\n[![NPM version](https://img.shields.io/npm/v/htmlparser2.svg)](https://npmjs.org/package/htmlparser2)\n[![Downloads](https://img.shields.io/npm/dm/htmlparser2.svg)](https://npmjs.org/package/htmlparser2)\n[![Node.js CI](https://github.com/fb55/htmlparser2/actions/workflows/nodejs-test.yml/badge.svg)](https://github.com/fb55/htmlparser2/actions/workflows/nodejs-test.yml)\n[![Coverage](https://img.shields.io/coveralls/fb55/htmlparser2.svg)](https://coveralls.io/r/fb55/htmlparser2)\n\nThe fast \u0026 forgiving HTML/XML parser.\n\n_htmlparser2 is [the fastest HTML parser](#performance), and takes some shortcuts to get there. If you need strict HTML spec compliance, have a look at [parse5](https://github.com/inikulin/parse5)._\n\n## Installation\n\n    npm install htmlparser2\n\nA live demo of `htmlparser2` is available [on AST Explorer](https://astexplorer.net/#/2AmVrGuGVJ).\n\n## Ecosystem\n\n| Name                                                          | Description                                             |\n| ------------------------------------------------------------- | ------------------------------------------------------- |\n| [htmlparser2](https://github.com/fb55/htmlparser2)            | Fast \u0026 forgiving HTML/XML parser                        |\n| [domhandler](https://github.com/fb55/domhandler)              | Handler for htmlparser2 that turns documents into a DOM |\n| [domutils](https://github.com/fb55/domutils)                  | Utilities for working with domhandler's DOM             |\n| [css-select](https://github.com/fb55/css-select)              | CSS selector engine, compatible with domhandler's DOM   |\n| [cheerio](https://github.com/cheeriojs/cheerio)               | The jQuery API for domhandler's DOM                     |\n| [dom-serializer](https://github.com/cheeriojs/dom-serializer) | Serializer for domhandler's DOM                         |\n\n## Usage\n\n`htmlparser2` itself provides a callback interface that allows consumption of documents with minimal allocations.\nFor a more ergonomic experience, read [Getting a DOM](#getting-a-dom) below.\n\n```js\nimport * as htmlparser2 from \"htmlparser2\";\n\nconst parser = new htmlparser2.Parser({\n    onopentag(name, attributes) {\n        /*\n         * This fires when a new tag is opened.\n         *\n         * If you don't need an aggregated `attributes` object,\n         * have a look at the `onopentagname` and `onattribute` events.\n         */\n        if (name === \"script\" \u0026\u0026 attributes.type === \"text/javascript\") {\n            console.log(\"JS! Hooray!\");\n        }\n    },\n    ontext(text) {\n        /*\n         * Fires whenever a section of text was processed.\n         *\n         * Note that this can fire at any point within text and you might\n         * have to stitch together multiple pieces.\n         */\n        console.log(\"--\u003e\", text);\n    },\n    onclosetag(tagname) {\n        /*\n         * Fires when a tag is closed.\n         *\n         * You can rely on this event only firing when you have received an\n         * equivalent opening tag before. Closing tags without corresponding\n         * opening tags will be ignored.\n         */\n        if (tagname === \"script\") {\n            console.log(\"That's it?!\");\n        }\n    },\n});\nparser.write(\n    \"Xyz \u003cscript type='text/javascript'\u003econst foo = '\u003c\u003cbar\u003e\u003e';\u003c/script\u003e\",\n);\nparser.end();\n```\n\nOutput (with multiple text events combined):\n\n```\n--\u003e Xyz\nJS! Hooray!\n--\u003e const foo = '\u003c\u003cbar\u003e\u003e';\nThat's it?!\n```\n\nThis example only shows three of the possible events.\nRead more about the parser, its events and options in the [wiki](https://github.com/fb55/htmlparser2/wiki/Parser-options).\n\n### Usage with streams\n\nWhile the `Parser` interface closely resembles Node.js streams, it's not a 100% match.\nUse the `WritableStream` interface to process a streaming input:\n\n```js\nimport { WritableStream } from \"htmlparser2/WritableStream\";\n\nconst parserStream = new WritableStream({\n    ontext(text) {\n        console.log(\"Streaming:\", text);\n    },\n});\n\nconst htmlStream = fs.createReadStream(\"./my-file.html\");\nhtmlStream.pipe(parserStream).on(\"finish\", () =\u003e console.log(\"done\"));\n```\n\n## Getting a DOM\n\nThe `DomHandler` produces a DOM (document object model) that can be manipulated using the [`DomUtils`](https://github.com/fb55/DomUtils) helper.\n\n```js\nimport * as htmlparser2 from \"htmlparser2\";\n\nconst dom = htmlparser2.parseDocument(htmlString);\n```\n\nThe `DomHandler`, while still bundled with this module, was moved to its [own module](https://github.com/fb55/domhandler).\nHave a look at that for further information.\n\n## Parsing Feeds\n\n`htmlparser2` makes it easy to parse RSS, RDF and Atom feeds, by providing a `parseFeed` method:\n\n```javascript\nconst feed = htmlparser2.parseFeed(content, options);\n```\n\n## Performance\n\nAfter having some artificial benchmarks for some time, **@AndreasMadsen** published his [`htmlparser-benchmark`](https://github.com/AndreasMadsen/htmlparser-benchmark), which benchmarks HTML parses based on real-world websites.\n\nAt the time of writing, the latest versions of all supported parsers show the following performance characteristics on GitHub Actions (sourced from [here](https://github.com/AndreasMadsen/htmlparser-benchmark/blob/e78cd8fc6c2adac08deedd4f274c33537451186b/stats.txt)):\n\n```\nhtmlparser2        : 2.17215 ms/file ± 3.81587\nnode-html-parser   : 2.35983 ms/file ± 1.54487\nhtml5parser        : 2.43468 ms/file ± 2.81501\nneutron-html5parser: 2.61356 ms/file ± 1.70324\nhtmlparser2-dom    : 3.09034 ms/file ± 4.77033\nhtml-dom-parser    : 3.56804 ms/file ± 5.15621\nlibxmljs           : 4.07490 ms/file ± 2.99869\nhtmljs-parser      : 6.15812 ms/file ± 7.52497\nparse5             : 9.70406 ms/file ± 6.74872\nhtmlparser         : 15.0596 ms/file ± 89.0826\nhtml-parser        : 28.6282 ms/file ± 22.6652\nsaxes              : 45.7921 ms/file ± 128.691\nhtml5              : 120.844 ms/file ± 153.944\n```\n\n## How does this module differ from [node-htmlparser](https://github.com/tautologistics/node-htmlparser)?\n\nIn 2011, this module started as a fork of the `htmlparser` module.\n`htmlparser2` was rewritten multiple times and, while it maintains an API that's mostly compatible with `htmlparser`, the projects don't share any code anymore.\n\nThe parser now provides a callback interface inspired by [sax.js](https://github.com/isaacs/sax-js) (originally targeted at [readabilitySAX](https://github.com/fb55/readabilitysax)).\nAs a result, old handlers won't work anymore.\n\nThe `DefaultHandler` was renamed to clarify its purpose (to `DomHandler`). The old name is still available when requiring `htmlparser2` and your code should work as expected.\n\nThe `RssHandler` was replaced with a `getFeed` function that takes a `DomHandler` DOM and returns a feed object. There is a `parseFeed` helper function that can be used to parse a feed from a string.\n\n## Security contact information\n\nTo report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).\nTidelift will coordinate the fix and disclosure.\n\n## `htmlparser2` for enterprise\n\nAvailable as part of the Tidelift Subscription.\n\nThe maintainers of `htmlparser2` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-htmlparser2?utm_source=npm-htmlparser2\u0026utm_medium=referral\u0026utm_campaign=enterprise\u0026utm_term=repo)\n","funding_links":["https://github.com/sponsors/fb55","https://tidelift.com/funding/github/npm/htmlparser2","https://tidelift.com/security","https://tidelift.com/subscription/pkg/npm-htmlparser2?utm_source=npm-htmlparser2\u0026utm_medium=referral\u0026utm_campaign=enterprise\u0026utm_term=repo"],"categories":["TypeScript","Repository","HTML","GIT 仓库","JavaScript","Uncategorized"],"sub_categories":["Parsing","解析工具","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffb55%2Fhtmlparser2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffb55%2Fhtmlparser2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffb55%2Fhtmlparser2/lists"}