Skip to main content

htmlparse: A basic HTML parser in Python

Installation

# Linux
python3 -m pip install parser-html
# Windows
python -m pip install parser-html
# Build from source
python -m pip install git+https://github.com/AaravMalani/htmlparse

Usage

import htmlparse

with open('index.html', 'r') as f:
    element = htmlparse.parse_html(f.read())
    if not element:
        raise ValueError("Parsing failed!")
print(element.children) # Sub-elements
print(element.innerHTML) # Data enclosed by tag
print(element.outerHTML) # Data enclosed by tag as well as the tag itself
element.innerHTML = 'e>' # Rebuilds this element and sets the innerHTML of all the parent elements
print(element.children) # ['e>'] (The HTMLText element is represented as a string literal)
print(element.children[0].text) # e> (Use HTMLText.outerHTML for an HTML escaped string (e>) however don't set it)
element.outerHTML = '<div class="black blue"><a href="https://github.com/" id="abc"></div>' # Read above statement
# assigning to element.children is in the works
print(tag.attrs) # {"href":"https://github.com/", "id":"abc"}
print(tag.tag_name) # a
element.children = []
element.attrs = {} # WARNING! You have to set it, you can't do element.attrs.update or element.attrs |=
print(tag.outerHTML) # <div></div>

ToDo

  • Support for CSS styles
  • Support for JS scripts
  • Support for assignment to HTMLElement.children list
  • Support for text between strings
  • Support for CSS selectors
  • Support for XPATH

Release files for parser-html 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parser-html 1.0.1
File Size Uploaded
parser-html-1.0.1.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parser-html 1.0.1
File Interpreter ABI Platform
parser_html-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / parser-html-1.0.1.tar.gz

Download URL parser-html-1.0.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6ebda2607cf3920862fa35c301421b7db436aedc59dbdfbb3605d398fad7e678
BLAKE2b-256 checksum
How to use checksums
a93b2522f1e1a839066a30539405f2e66a372f31eb896bd4b3d44d3ade871f01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / parser_html-1.0.1-py3-none-any.whl

Download URL parser_html-1.0.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cac0f0bbe9a3fd09db50ab8f6ef7582f6ecacbd89f1a0eb19c28738eadc3bda
BLAKE2b-256 checksum
How to use checksums
f5e316f96ea6ff0ed57bcaee7b2c2a0b4cb6e868cef53cc59c438f2f48523cef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page