All Projects → fb55 → Htmlparser2

fb55 / Htmlparser2

Licence: mit
The fast & forgiving HTML and XML parser

Programming Languages

typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to Htmlparser2

Skrape.it
A Kotlin-based testing/scraping/parsing library providing the ability to analyze and extract data from HTML (server & client-side rendered). It places particular emphasis on ease of use and a high level of readability by providing an intuitive DSL. It aims to be a testing lib, but can also be used to scrape websites in a convenient fashion.
Stars: ✭ 231 (-93%)
Mutual labels:  hacktoberfest, html-parser, dom
Didom
Simple and fast HTML and XML parser
Stars: ✭ 1,939 (-41.22%)
Mutual labels:  html-parser, dom, xml
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (-95.51%)
Mutual labels:  hacktoberfest, xml
Home
A configurable and eXtensible Xml serializer for .NET.
Stars: ✭ 208 (-93.7%)
Mutual labels:  hacktoberfest, xml
Horaires Ratp Api
Webservice pour les horaires et trafic RATP en temps réel
Stars: ✭ 232 (-92.97%)
Mutual labels:  hacktoberfest, xml
Floki
Floki is a simple HTML parser that enables search for nodes using CSS selectors.
Stars: ✭ 1,642 (-50.23%)
Mutual labels:  hacktoberfest, html-parser
Js2xml
Convert Javascript code to an XML document
Stars: ✭ 124 (-96.24%)
Mutual labels:  hacktoberfest, xml
Xbmc
Kodi is an award-winning free and open source home theater/media center software and entertainment hub for digital media. With its beautiful interface and powerful skinning engine, it's available for Android, BSD, Linux, macOS, iOS and Windows.
Stars: ✭ 13,175 (+299.36%)
Mutual labels:  hacktoberfest, xml
html5parser
A super tiny and fast html5 AST parser.
Stars: ✭ 153 (-95.36%)
Mutual labels:  dom, html-parser
AdvancedHTMLParser
Fast Indexed python HTML parser which builds a DOM node tree, providing common getElementsBy* functions for scraping, testing, modification, and formatting. Also XPath.
Stars: ✭ 90 (-97.27%)
Mutual labels:  dom, html-parser
fox
A Fortran XML library
Stars: ✭ 51 (-98.45%)
Mutual labels:  dom, xml
Sdformat
Simulation Description Format (SDFormat) parser and description files.
Stars: ✭ 51 (-98.45%)
Mutual labels:  hacktoberfest, xml
Sinuous
🧬 Light, fast, reactive UI library
Stars: ✭ 740 (-77.57%)
Mutual labels:  hacktoberfest, dom
Woodstox
The gold standard Stax XML API implementation. Now at Github.
Stars: ✭ 145 (-95.6%)
Mutual labels:  hacktoberfest, xml
Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (-80.66%)
Mutual labels:  hacktoberfest, xml
Parsel
Parsel lets you extract data from XML/HTML documents using XPath or CSS selectors
Stars: ✭ 628 (-80.96%)
Mutual labels:  hacktoberfest, xml
xmlresolver
The xmlresolver project provides an advanced implementation of the SAX EntityResolver (and extended EntityResolver2), the Transformer URIResolver, the DOM LSResourceResolver, the StAX XMLResolver, and a new NamespaceResolver. It uses the OASIS XML Catalogs V1.1 Standard to provide a mapping from external identifiers and URIs to local resources.
Stars: ✭ 31 (-99.06%)
Mutual labels:  dom, xml
Anglesharp
👼 The ultimate angle brackets parser library parsing HTML5, MathML, SVG and CSS to construct a DOM based on the official W3C specifications.
Stars: ✭ 4,018 (+21.79%)
Mutual labels:  hacktoberfest, dom
Zek
Generate a Go struct from XML.
Stars: ✭ 451 (-86.33%)
Mutual labels:  hacktoberfest, xml
html-parser
A simple and general purpose html/xhtml parser, using Pest.
Stars: ✭ 56 (-98.3%)
Mutual labels:  dom, html-parser

htmlparser2

NPM version Downloads Build Status Coverage

The fast & forgiving HTML/XML parser.

Installation

npm install htmlparser2

A live demo of htmlparser2 is available here.

Ecosystem

Name Description
htmlparser2 Fast & forgiving HTML/XML parser
domhandler Handler for htmlparser2 that turns documents into a DOM
domutils Utilities for working with domhandler's DOM
css-select CSS selector engine, compatible with domhandler's DOM
cheerio The jQuery API for domhandler's DOM
dom-serializer Serializer for domhandler's DOM

Usage

htmlparser2 itself provides a callback interface that allows consumption of documents with minimal allocations. For a more ergonomic experience, read Getting a DOM below.

const htmlparser2 = require("htmlparser2");
const parser = new htmlparser2.Parser({
    onopentag(name, attributes) {
        /*
         * This fires when a new tag is opened.
         *
         * If you don't need an aggregated `attributes` object,
         * have a look at the `onopentagname` and `onattribute` events.
         */
        if (name === "script" && attributes.type === "text/javascript") {
            console.log("JS! Hooray!");
        }
    },
    ontext(text) {
        /*
         * Fires whenever a section of text was processed.
         *
         * Note that this can fire at any point within text and you might
         * have to stich together multiple pieces.
         */
        console.log("-->", text);
    },
    onclosetag(tagname) {
        /*
         * Fires when a tag is closed.
         *
         * You can rely on this event only firing when you have received an
         * equivalent opening tag before. Closing tags without corresponding
         * opening tags will be ignored.
         */
        if (tagname === "script") {
            console.log("That's it?!");
        }
    },
});
parser.write(
    "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>"
);
parser.end();

Output (with multiple text events combined):

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

This example only shows three of the possible events. Read more about the parser, its events and options in the wiki.

Usage with streams

While the Parser interface closely resembles Node.js streams, it's not a 100% match. Use the WritableStream interface to process a streaming input:

const { WritableStream } = require("htmlparser2/lib/WritableStream");
const parserStream = new WritableStream({
    ontext(text) {
        console.log("Streaming:", text);
    },
});

const htmlStream = fs.createReadStream("./my-file.html");
htmlStream.pipe(parserStream).on("finish", () => console.log("done"));

Getting a DOM

The DomHandler produces a DOM (document object model) that can be manipulated using the DomUtils helper.

const htmlparser2 = require("htmlparser2");

const dom = htmlparser2.parseDocument(htmlString);

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

Parsing RSS/RDF/Atom Feeds

const feed = htmlparser2.parseFeed(content, options);

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 GitHub Actions (sourced from here):

htmlparser2        : 2.17215 ms/file ± 3.81587
node-html-parser   : 2.35983 ms/file ± 1.54487
html5parser        : 2.43468 ms/file ± 2.81501
neutron-html5parser: 2.61356 ms/file ± 1.70324
htmlparser2-dom    : 3.09034 ms/file ± 4.77033
html-dom-parser    : 3.56804 ms/file ± 5.15621
libxmljs           : 4.07490 ms/file ± 2.99869
htmljs-parser      : 6.15812 ms/file ± 7.52497
parse5             : 9.70406 ms/file ± 6.74872
htmlparser         : 15.0596 ms/file ± 89.0826
html-parser        : 28.6282 ms/file ± 22.6652
saxes              : 45.7921 ms/file ± 128.691
html5              : 120.844 ms/file ± 153.944

How does this module differ from node-htmlparser?

In 2011, this module started as a fork of the htmlparser module. htmlparser2 was rewritten multiple times and, while it maintains an API that's mostly compatible with htmlparser in most cases, the projects don't share any code anymore.

The parser now provides a callback interface inspired by 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.

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

htmlparser2 for enterprise

Available as part of the Tidelift Subscription

The 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].