https://github.com/aaravmalani/htmlparse
A basic HTML parser in Python
https://github.com/aaravmalani/htmlparse
collaborate html module package parser pip pypi python re recursive regex
Last synced: 25 days ago
JSON representation
A basic HTML parser in Python
- Host: GitHub
- URL: https://github.com/aaravmalani/htmlparse
- Owner: AaravMalani
- License: mit
- Created: 2023-04-08T18:21:24.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-04-14T10:31:31.000Z (about 2 years ago)
- Last Synced: 2025-03-22T00:11:16.235Z (about 1 month ago)
- Topics: collaborate, html, module, package, parser, pip, pypi, python, re, recursive, regex
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# htmlparse: A basic HTML parser in Python
## Installation
```sh
# 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
```py
import htmlparsewith open('index.html', 'r') as f:
' # Read above statement
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 = '
# 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) #
```## ToDo
- [ ] Support for CSS styles
- [ ] Support for JS scripts
- [x] Support for assignment to `HTMLElement.children` list
- [x] Support for text between strings
- [ ] Support for CSS selectors
- [ ] Support for XPATH