Skip to main content

PyPi version PyPi pyversions PyPi license

Convert a HTML webpage to JSON data using a template defined in JSON.

Installation Guide

This package is available on PyPi. Just use pip install -U html2json to install it. Then you can import it using from html2json import collect.

  • Note that starting version 0.3.0, at least Python 3.9 is required.

API

The method is collect(html, template). html is the HTML of page loaded as string, and template is the JSON of template loaded as Python objects.

Note that the HTML must contain the root node, like <html>...</html> or <div>...</div>.

Template Syntax

| For detailed syntax examples, please refer to unit tests (with 100% coverage).

The basic syntax is keyName: [selector, attr, [listOfRegexes]]. 1. selector is a CSS selector (supported by lxml). - When the selector is null, the root node itself is matched. - When the selector cannot be matched, null is returned. - When the selector matches single element, a string is returned. - When the selector matches multiple elements, a list of string is returned. - If only selector is needed, you can just specify a string instead of list. 2. attr matches the attribute value. It can be null to match either the inner text or the outer text when the inner text is empty. - Optional when only selector is needed. 3. The list of regexes [listOfRegexes] supports two forms of regex operations. The operations with in the list are executed sequentially. - Replacement: s/regex/replacement/g. g is optional for multiple replacements. - Extraction: /regex/. - Note that you can use any character as separator instead of /. - Optional when only selector and/or attribute are needed.

For example:

{
    "Color": ["head link:nth-of-type(1)", "href", ["/\\w+(?=\\.css)/"]],
}

Starting version 0.3.1, besides value, key can also matched like "[selector, ...]": .... Note that key must be a string for valid JSON.

  • When the selector cannot be matched, key is not added to JSON.
  • When the selector matches single element, returned string is used as key.
  • When the selector matches multiple elements, list of returned strings are used as multiple keys.

Starting version 0.3.1, you can also replace certain part of value's selector with current key using syntax ...{key}.... This is especially useful when key is dynamic.


As JSON, nested structure can be easily constructed.

{
    "Cover": {
        "URL": [".cover img", "src", []],
        "Number of Favorites": [".cover .favorites", "value", []]
    },
}

An alternative simplified syntax keyName: [subRoot, subTemplate] can be used. 1. subRoot a CSS selector of the new root for each sub entry. 2. subTemplate is a sub-template for each entry, recursively.

For example, the previous example can be simplified as follow.

{
    "Cover": [".cover", {
        "URL": ["img", "src", []],
        "Number of Favorites": [".favorites", "value", []]
    }],
}

To extract a list of sub-entries following the same sub-template, the list syntax is keyName: [[subRoot, subTemplate]]. Please note the difference (surrounding [ and ]) from the previous syntax above. 1. subRoot is the CSS selector of the new root for each sub entry. 2. subTemplate is the sub-template for each entry, recursively. - Optional or null to match entire sub-root

For example:

{
    "Comments": [[".comments", {
        "From": [".from", null, []],
        "Content": [".content", null, []],
        "Photos": [["img", {
            "URL": ["", "src", []]
        }]]
    }]]
}

Release files for html2json 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for html2json 0.3.3
File Size Uploaded
html2json-0.3.3.tar.gz 63.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for html2json 0.3.3
File Interpreter ABI Platform
html2json-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 68.5 kB

Release files / html2json-0.3.3.tar.gz

Download URL html2json-0.3.3.tar.gz
Size 63.3 kB
Tags Source
SHA-256 checksum
How to use checksums
64431862c841ecbd0a3e7b28a851f4ecd2c73a4e4a9737a9d850291a16310f4c
BLAKE2b-256 checksum
How to use checksums
829c995644e96067634b9470499a8a12f282c6312edcb90d83492bc5d95b6036
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.9

Release files / html2json-0.3.3-py3-none-any.whl

Download URL html2json-0.3.3-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7079d10d626bb95606363135f2fb0373aa4e7109b2492a9901608a953b431e9e
BLAKE2b-256 checksum
How to use checksums
52872ca8306a71c49348afacd8daec1b17324d2f3aad129b033644ade285bd74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.9

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page