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(item: Node) -> Optional[Node]:
    if item.name == "h1":
        item.children = [TextNode(value="New Title")]
        
    return item

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.0.tar.gz (6.6 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.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dompa-0.4.0.tar.gz
  • Upload date:
  • Size: 6.6 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.0.tar.gz
Algorithm Hash digest
SHA256 ee4d920019d1214b6ccf9a38e9ef77bbc9735b7bc37437131bc29c2a2b8a3b8e
MD5 b3bb947d536bc85e30473711005ac143
BLAKE2b-256 024fd40ff8abd2277c03668d21e59bcfa5415946bc138f26842f40501804b4cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dompa-0.4.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc9bbbf79f1d23421fda1ad5358e0b90c1153f50ee6dc0242f178e340fab88ad
MD5 82378d6a8d9a8bad816734ecece4244a
BLAKE2b-256 c576669330addfbda804479bb2b9ba266512e17fc91ebd424584501bbd47fcdd

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