npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hext

v11.0.12

Published

Domain-specific language for extracting structured data from HTML

Downloads

317

Readme

Hext — Extract Data from HTML

Hext Logo

Hext is a domain-specific language for extracting structured data from HTML. It can be thought of as a counterpart to templates, which are typically used by web developers to structure content on the web.

A Quick Example

The following Hext template collects all hyperlinks and extracts the href and the clickable text.

<a href:link @text:title />

Hext does so by recursively trying to match every HTML element. In the case above, an element is required to have the tag a and an attribute called href. If the element matches, its attribute href and its textual representation are stored as link and title, respectively.

If the above Hext template is applied to this piece of HTML:

<body>
  <a href="one.html">  Page 1</a>
  <a href="two.html">  Page 2</a>
  <a href="three.html">Page 3</a>
</body>

Hext will produce the following values:

{ "link": "one.html",   "title": "Page 1" },
{ "link": "two.html",   "title": "Page 2" },
{ "link": "three.html", "title": "Page 3" }

You can use this example in Hext’s live code editor. Visit Hext’s documentation and its section “How Hext Matches Elements” for a more thorough explanation.

Compatibility

This binary package is compatible with:

  • Node v18, v20, v22, v23
  • Linux x86_64 (GLIBC ≥2.14, basically any distribution built after the year 2012)
  • macOS x86_64 (10.11 El Capitan or later)
  • macOS ARM64 (11.0 Big Sur or later)

If you would like to use Hext on a system that is not supported, please raise an issue on github: https://github.com/html-extract/hext/issues/.

Using Hext with Node

The module exposes three interfaces:

  • var html = new hext.Html("<html>...</html>") -> internal object
  • var rule = new hext.Rule("...") -> internal object
  • var result = rule.extract(html) -> Array of plain JS-objects
  • rule.extract has a second optional parameter max_searches which is of type unsigned int. The search for matching elements is aborted after this limit is reached. The default is 0, which never aborts.
const hext = require('hext');
const request = require('request');


// hext.Rule's constructor expects a single argument
// containing a Hext template.
// Throws an Error on invalid syntax, with
// Error.message containing the error description.
const rule = new hext.Rule(`
<tr>
  <td><span @text:rank /></td>
  <td><a href:href @text:title /></td>
</tr>
<?tr>
  <td>
    <span @text:score />
    <a @text:user />
    <a:last-child @text:filter(/\\d+/):comment_count />
  </td>
</tr>`)


request('https://news.ycombinator.com/',
        function(error, response, body) {

  // hext.Html's constructor expects a single argument
  // containing a UTF-8 encoded string of HTML.
  const html = new hext.Html(body);

  // hext.Rule.extract expects an argument of type
  // hext.Html. Returns an Array containing Objects
  // which contain key-value pairs of type String.
  const results = rule.extract(html)

  // Print each rule match as JSON
  results.forEach(result => console.log(JSON.stringify(result)));
});

More

Hext is also available for Python:

pip install hext

The Python distribution also contains the htmlext command-line utility.

In addition to Python and Node, there are language bindings for JavaScript, PHP and Ruby.

License

Hext is released under the terms of the Apache License v2.0. The source code is hosted on Github. This binary package includes content authored by third parties: