Skip to main content

Tools for converting HTMLs to dicts & calculating diff between them

Project description

diff4html

Tools for converting HTMLs to dicts & calculating diff between them.

🔎 What for?

Well, if you need to calculate the diff between HTML pages without inplace markup (with <ins> & <del> tags), or maybe you experience issues with matching these tags later, or even find the htmldiff's behavior not entirely accurate, then diff4html may be a great fit for you.

Unlike htmldiff, the result here is a difference object. This object is a list containing information about each change. Each change, in turn, is described by the start and end indices of the modification in the original HTML page, along with the subject of the change.

Application of this is fully handled by diff4html and can be triggered via simple Python API, so you don't have to restore snapshots on your own.

🛠️ Installation

pip install diff4html

⚡️ Usage

Let's start with running IPython or Jupyter and making two HtmlDicts for our pages. The one for an example.org page with <head> tag ignored and the other for slightly modified version of it:

HtmlDict here is simply a representation of HTML tree built on native Python dict & list data types.

In [1]: import requests
        from diff4html import HtmlDict

        page_1 = HtmlDict(
            requests.get("https://example.org").text,
            ignore=["head"] # NOTICE: we ignore <head>...</head> part here
        )
        page_2 = HtmlDict("""
            <html>
                <body>
                    <div>
                        <h1>Example Domain modified</hi> <!-- NOTICE: changed text-->
                        <!-- NOTICE: missing rest -->
                    </div>
                </body>
            </html>
        """)
        page_2
Out[1]: HtmlDict({...})

Let's then calculate diff between them. For example: I don't want to store the whole page 2 source code and want only delta to remain.

In [2]: diff = page_2 - page_1
        diff
/.../diff4html/diff.py: UserWarning: ignored tags of both objects don't match
Out[2]: HtmlDiff([...])

What this code does is it determines which parts of page 1 were deleted, modified, and added in page 2, and returns information about all these changes in the form of an HtmlDiff object.

HtmlDiff, in the context of page 2 - page 1, is a list of changes in the form of (s, e, t), where s and e are the start and end indices pointing to the differing element in page 1, and t is the content of the corresponding differing element in page 2.

Also note that a warning is displayed along with the result, indicating that both objects contain different sets of ignored tags. This happened because when creating page_1, we ignored the <head> tag, while when creating page_2, we didn’t specify any tags to ignore at all.

If one day I want to restore the entire source code of the page 2 I can do the following. We can check their equality right away:

In [3]: page_2_restored = page_1 + diff # you can think of it as: 
        # page_1 + (page_2 - page_1) => page_1 + page_2 - page_1 => page_2
        page_2_restored == page_2
Out[3]: True

By the way, there is a hash mechanism under the hood that protects the delta to be applied to any random html:

In [4]: page_2 + diff # diff can be applied to page_1 only
Out[4]: ValueError: wrong snapshot used for applying diff

And if I want to use lxml after all here's a pretty straight workaround for it:

In [5]: page_2_restored.to_lxml()
Out[5]: <Element div at 0x000000000>

Project details


Download files

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

Source Distribution

diff4html-1.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

diff4html-1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file diff4html-1.0.tar.gz.

File metadata

  • Download URL: diff4html-1.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for diff4html-1.0.tar.gz
Algorithm Hash digest
SHA256 cd9abb38e2f1671f2288c8f199f6ab1af839c5c238958144738308059df31eeb
MD5 b51e8ef515f7f9eb56c0ad5201bd620d
BLAKE2b-256 c68ae41eae96723fff15f9d92c7fd4eecde1a091921823227f5066b3000330fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for diff4html-1.0.tar.gz:

Publisher: publish.yml on dsp-shp/diff4html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file diff4html-1.0-py3-none-any.whl.

File metadata

  • Download URL: diff4html-1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for diff4html-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a26c412e78bbf5fae31cdda2d1019660256de9e11f967c255a76000a9cb2296
MD5 cf98471149ec9b5ebb3e16bf6c5fa0a8
BLAKE2b-256 a7d0837e5b5ed5a79610ea6268841ae4d174f2782fe74b0ed96de745970b5b26

See more details on using hashes here.

Provenance

The following attestation bundles were made for diff4html-1.0-py3-none-any.whl:

Publisher: publish.yml on dsp-shp/diff4html

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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