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.
Installing
With pip:
$ pip install fa-htmlstream
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fa_htmlstream-1.2.0.tar.gz.
File metadata
- Download URL: fa_htmlstream-1.2.0.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d54da036f10b219ecf4e9b407252c101dbeddd8c3e4dda93dde937780e4592f8
|
|
| MD5 |
ced3b6ace5bbdaed0043800b5c707be4
|
|
| BLAKE2b-256 |
00c150e282828ede746a586356bae8a3b45416456c2d122a738dec574c02dbc3
|
Provenance
The following attestation bundles were made for fa_htmlstream-1.2.0.tar.gz:
Publisher:
python-publish.yml on faboo/htmlstream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fa_htmlstream-1.2.0.tar.gz -
Subject digest:
d54da036f10b219ecf4e9b407252c101dbeddd8c3e4dda93dde937780e4592f8 - Sigstore transparency entry: 701522741
- Sigstore integration time:
-
Permalink:
faboo/htmlstream@f27680fa63a81e675d3405cb2c13067c6e675310 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/faboo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@f27680fa63a81e675d3405cb2c13067c6e675310 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fa_htmlstream-1.2.0-py3-none-any.whl.
File metadata
- Download URL: fa_htmlstream-1.2.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb295906674620a798b5d93d38da97471a27a5f2cf65370bbb101381dbbb2a5b
|
|
| MD5 |
89c4ce8ebb1ddd764bf64fe721df94c6
|
|
| BLAKE2b-256 |
a63cb058c6d35518710e0d5eb45707ecaf800cf2d8eb66cf947f18bb5ab3fd74
|
Provenance
The following attestation bundles were made for fa_htmlstream-1.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on faboo/htmlstream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fa_htmlstream-1.2.0-py3-none-any.whl -
Subject digest:
cb295906674620a798b5d93d38da97471a27a5f2cf65370bbb101381dbbb2a5b - Sigstore transparency entry: 701522743
- Sigstore integration time:
-
Permalink:
faboo/htmlstream@f27680fa63a81e675d3405cb2c13067c6e675310 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/faboo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@f27680fa63a81e675d3405cb2c13067c6e675310 -
Trigger Event:
release
-
Statement type: