Skip to main content

A HTML5 parser.

Project description

Dompa

Coverage

A work-in-progress HTML5 document parser. It takes an input of an HTML string, parses it into a node tree, and provides an API for querying and manipulating the node tree.

Install

pip install dompa

Usage

The most basic usage looks like this:

from dompa import Dompa

dom = Dompa("<div>Hello, World</div>")

# Get the tree of nodes
nodes = dom.nodes()

# Get the HTML string
html = dom.html()

DOM manipulation

You can run queries on the node tree to get or manipulate node(s).

find

You can find nodes with the find method which takes a Callable that gets Node passed to it and that has to return a boolean true or false, like so:

from dompa import Dompa

dom = Dompa("<h1>Site Title</h1><ul><li>...</li><li>...</li></ul>")
list_items = dom.find(lambda n: n.name == "li")

All nodes returned with find are deep copies, so mutating them has no effect on Dompa's state.

update

You can update nodes with the update method which takes a Callable that gets a Node passed to it, and has to return the updated node, like so:

from typing import Optional
from dompa import Dompa
from dompa.nodes import Node, TextNode

dom = Dompa("<h1>Site Title</h1><ul><li>...</li><li>...</li></ul>")

def update_title(node: Node) -> Optional[Node]:
    if node.name == "h1":
        node.children = [TextNode(value="New Title")]
        
    return node

dom.update(update_title)

If you wish to remove a node then return None instead of the node.

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

dompa-0.4.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

dompa-0.4.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file dompa-0.4.1.tar.gz.

File metadata

  • Download URL: dompa-0.4.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for dompa-0.4.1.tar.gz
Algorithm Hash digest
SHA256 cca19de90218dea41301298ffc26d26b1692c36f80a95fc05840275dbb68ab63
MD5 71a5dcfa501c89a806196fb6d17e5d6d
BLAKE2b-256 30c37a41de68c038b774955d06e1bfe221ccff92ebe1ed6587a966bf0e79f34b

See more details on using hashes here.

File details

Details for the file dompa-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: dompa-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for dompa-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e443e9e6d306074c18c4175b88f25a2f4824ca4d03d9264a4ee2e3ae24e6edb
MD5 7f270702eaeafaf216d6bd36798e0b0a
BLAKE2b-256 fd3b1327c4e02544b2719d89ed2dd34a5330d78b93a7bdedb0f9b602ace649bf

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