Skip to main content

A simple streaming HTML parser

Project description

HTMLStream is a simple stream parser for HTML. Rather than load an entire DOM into memory all at once, HTMLStream reads a io stream incrementally, resulting in a stream of HTML tags and text. The aim is to be fairly permissible, generating usable results from even malformed HTML.

Examples

Find the doc type of a document:

from htmlstream import Parses, DocType, Text

def getDocType(filename:str) -> str:
    with open(filename, encoding='utf-8') as file:
        for node in Parser(file):
            if isinstance(node, Text): continue

            if isinstance(node, DocType):
                return node.doctype
            else:
                raise Exception('No doctype!')

Extract all the text:

from htmlstream import Parses, Text

def getAllText(filename:str) -> str:
    text = ''

    with open(filename, encoding='utf-8') as file:
        for node in Parser(file):
            if isinstance(node, Text):
                text += node.text

    return text

Get the text of a specific element:

from htmlstream import Parses, OpenTag, Text

def getElementText(filename:str, eid:str) -> str:
    inElement = False
    with open(filename, encoding='utf-8') as file:
        for node in Parser(file):
            if inElement and isinstance(node, Text):
                return node.text
            if isinstance(node, OpenTag) and node.attributes.get('id') == eid):
                inElement = True

    return 'MISSING'

API

class Parser(Iterator[Node])

The parser itself. The parser is an iterator, so it can be used if for loops, passed to list() and next(), and used in list comprehensions.

__init__(stream, maxTextLength=None)
stream TextIOBase:

The text stream to parse (e.g. a file object).

maxTextLength int|None:

The maximum length of a text node; unlimited if None

__next__() -> Node

Get the next node in the stream.

__iter__() -> Iterator[Node

The Parser itself.

class Node

Base node class.

class Text(Node)

A text node. This includes any and all text between tags, comments, and doctypes.

text str:

The content of the section of text.

class Comment(Node)

A comment node. All the text between the opening <!– and closing –>.

comment str:

The content of the comment.

class DocType(Node)

A doctype node.

doctype str:

The specific doctype declared (i.e. “html”).

class Tag(Node)

Base tag class.

tag str:

The name of the tag (e.g. “p”, “table”, “body”, etc.).

class OpenTag(Tag)

An opening tag, including unclosed and self-closing tags.

selfClosing bool:

True if the tag is self-closing (for example <br />).

attributes dict[str,str|None]:

The attributes included in tag. Toggle attributes have the value None.

class CloseTag(Tag)

A closing tag.

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

fa_htmlstream-1.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

fa_htmlstream-1.1.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file fa_htmlstream-1.1.0.tar.gz.

File metadata

  • Download URL: fa_htmlstream-1.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fa_htmlstream-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b34f26b686f31a6cb7b3150a300e6e8da790c2af0c7fdc8fb831df1b0071f489
MD5 0d7dfe56fbe716463b311cc3dedc2068
BLAKE2b-256 c32b00ee5ee34d62ff0b1569c78d2a5c800be6df6eabdfff7ed404e75ba41387

See more details on using hashes here.

Provenance

The following attestation bundles were made for fa_htmlstream-1.1.0.tar.gz:

Publisher: python-publish.yml on faboo/htmlstream

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

File details

Details for the file fa_htmlstream-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: fa_htmlstream-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fa_htmlstream-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e25610a5e1453ed10fb9cb9d8c022d37f1ae9b92ec8035b27b422591227c9480
MD5 5b2bff4a4d7172f12cc88019a7d16767
BLAKE2b-256 9955d90775662d0d31f8f2987afe52d8bdf3da40771c0a451e4b4a496339a80e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fa_htmlstream-1.1.0-py3-none-any.whl:

Publisher: python-publish.yml on faboo/htmlstream

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