Skip to main content

A module and command-line utility to extract structured data from HTML

Project description

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.

Components

This package includes:

  • The Hext Python module
  • The htmlext command-line utility

Using Hext with Python

The module exposes three interfaces:

  • html = hext.Html("<html>...</html>") -> object
  • rule = hext.Rule("...") -> object
  • rule.extract(html) -> dictionary of {string -> string}
  • 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.
import hext
import requests
import json

res = requests.get('https://news.ycombinator.com/')
res.raise_for_status()

# hext.Html's constructor expects a single argument
# containing an UTF-8 encoded string of HTML.
html = hext.Html(res.text)

# hext.Rule's constructor expects a single argument
# containing a Hext template.
# Throws an exception of type ValueError on invalid syntax.
rule = 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>""")

# hext.Rule.extract expects an argument of type hext.Html.
# Returns a list of dictionaries.
result = rule.extract(html)

# Print each dictionary as JSON
for map in result:
    print(json.dumps(map, ensure_ascii=False,
                          separators=(',',':')))

Using Hext on the Command Line

Hext ships with a command line utility called htmlext, which applies Hext templates to HTML documents and outputs JSON.

htmlext - Extract structured content from HTML.

Usage:
  htmlext [options] <hext-file> <html-file...>
      Apply extraction rules from <hext-file> to each
      <html-file> and print the captured content as JSON.

Options:
  -x [ --hext ] <file>  Add Hext from file
  -i [ --html ] <file>  Add HTML from file
  -s [ --str ] <string> Add Hext from string
  -c [ --compact ]      Print one JSON object per line
  -p [ --pretty ]       Pretty-print JSON
  -a [ --array ]        Wrap results in a JSON array
  -f [ --filter ] <key> Print values whose name matches <key>
  -l [ --lint ]         Do Hext syntax check
  -h [ --help ]         Print this help message
  -V [ --version ]      Print info and version

Ever wanted to watch the submissions on /r/videos in vlc? Well, take a look at this little guy right here:

htmlext \
  -i <(wget -O- -o/dev/null "https://old.reddit.com/r/videos/") \
  -s '<a class="title" href:x />' \
  -f x \
  | xargs vlc

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:

  • gumbo-parser. Copyright 2010 Google Inc. See gumbo.license.
  • rapidjson. Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. See rapidjson.license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

hext-1.0.2-cp310-cp310-manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10

hext-1.0.2-cp310-cp310-macosx_10_11_x86_64.whl (726.8 kB view details)

Uploaded CPython 3.10macOS 10.11+ x86-64

hext-1.0.2-cp39-cp39-manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9

hext-1.0.2-cp39-cp39-macosx_10_11_x86_64.whl (726.9 kB view details)

Uploaded CPython 3.9macOS 10.11+ x86-64

hext-1.0.2-cp38-cp38-manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8

hext-1.0.2-cp38-cp38-macosx_10_11_x86_64.whl (726.8 kB view details)

Uploaded CPython 3.8macOS 10.11+ x86-64

hext-1.0.2-cp37-cp37m-manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m

hext-1.0.2-cp37-cp37m-macosx_10_11_x86_64.whl (726.8 kB view details)

Uploaded CPython 3.7mmacOS 10.11+ x86-64

hext-1.0.2-cp36-cp36m-manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m

hext-1.0.2-cp36-cp36m-macosx_10_11_x86_64.whl (726.9 kB view details)

Uploaded CPython 3.6mmacOS 10.11+ x86-64

File details

Details for the file hext-1.0.2-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp310-cp310-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.10
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 79c3554b6dff6f2ea2b3f6aab093d4f83352ef2371d97917eb00785e5fe57124
MD5 55018bc59ba3b2d54d655776bad0575e
BLAKE2b-256 bb305e22b6b685e666b34a5640039399ff47717a9fa942042d2f70ffe116a26c

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp310-cp310-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp310-cp310-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 726.8 kB
  • Tags: CPython 3.10, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp310-cp310-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 74ac0fb615819da600c1214ab353eacaf79e61cad7243794b40080271d8e709c
MD5 9f91130d279453c3d6895b0733bb9bde
BLAKE2b-256 74a4a5a2f780eb3b07a66a07169d1b072e6dfd23b639a55c589180b67dc296a6

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8f3459ad8c460a7220d89d19657ca88cb888ae1bb9e9d6f15e19684f6ac1eaa4
MD5 4e91a2fe530ab7c44e2d3bbb0d5b8fc8
BLAKE2b-256 8454094c4ef28db8f199f3225dcda28417a03046b6e54bf420837cdd59e24ace

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp39-cp39-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp39-cp39-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 726.9 kB
  • Tags: CPython 3.9, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp39-cp39-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 12cd932c198791d2e04806280725b5f3537e2775ca4aa49d116e46007ea5d4a1
MD5 c3e9af4d70daa3890609dc57c44ae0e8
BLAKE2b-256 6ab59140474cdb1565f97f3921376a6238b62409e4052356bdbbdabb83d55ab2

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0593d84640dd21208b29bd2e39df88061724abc9320ac76afe22c519aed94c5a
MD5 7136814accc28134ed862feef0977dd3
BLAKE2b-256 85eadf8bcd025a8ad772419be552370b37b43104b729f41d05d4a53600e22457

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp38-cp38-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp38-cp38-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 726.8 kB
  • Tags: CPython 3.8, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp38-cp38-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 b646d6ad3cabb2739c68cc0f43386baeaa96c0cad4e7af1bf91e38f782bf0cb3
MD5 01ef053987f92c93b4a0e1bec05f7c50
BLAKE2b-256 870d5ad00ad5bbb38009df364bac0d3bbf467b5aa62b543da946481030a643ff

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d5ac148ed528419558020c1e84a955a7b970161213636850e3111ffc5cf04007
MD5 7e1a44b9ead6710475b8a68efe705ce5
BLAKE2b-256 589f16716c632634756f8db8332eef639558e47927e730745bf100a74d6592ec

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp37-cp37m-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp37-cp37m-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 726.8 kB
  • Tags: CPython 3.7m, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp37-cp37m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 68957986311a8df865210afea9e9f17fc06034c035e7cb37245f73dd5e6f723e
MD5 d2d571eb6d4ae1229b2dadd0bf0e20ec
BLAKE2b-256 c4500dfe7a71fddd8a2ae64671be911908f7a618a8b5b9cd6485f5122cd7da22

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cc0938260c42c1e85756c882e30df54c50a1e675423d76cfc774c1b47a01bd56
MD5 cb28ac21a8f012c5c5acd3b230920685
BLAKE2b-256 2f4c944c295f430afd63740222ef4978c9911a60114d96da32cd97502bf2bf53

See more details on using hashes here.

File details

Details for the file hext-1.0.2-cp36-cp36m-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: hext-1.0.2-cp36-cp36m-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 726.9 kB
  • Tags: CPython 3.6m, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for hext-1.0.2-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 53ef42ee73490e6724b59d5cf42289da9e8d4684c159382cc5560c64028e3a04
MD5 bddd39518667ae433d24448c4a9604cd
BLAKE2b-256 66d2752cbba8f38760d553f7732e0f86d58bf3509b9bcb38e80c86e73355de87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page