Skip to main content

A basic HTML parser in Python

Project description

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

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

parser-html-1.0.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

parser_html-1.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file parser-html-1.0.1.tar.gz.

File metadata

  • Download URL: parser-html-1.0.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for parser-html-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6ebda2607cf3920862fa35c301421b7db436aedc59dbdfbb3605d398fad7e678
MD5 6015faa40556b5642704ac60aec8545e
BLAKE2b-256 a93b2522f1e1a839066a30539405f2e66a372f31eb896bd4b3d44d3ade871f01

See more details on using hashes here.

File details

Details for the file parser_html-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: parser_html-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for parser_html-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cac0f0bbe9a3fd09db50ab8f6ef7582f6ecacbd89f1a0eb19c28738eadc3bda
MD5 2adf38cb158051cfe52c42001282c795
BLAKE2b-256 f5e316f96ea6ff0ed57bcaee7b2c2a0b4cb6e868cef53cc59c438f2f48523cef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page