Features
- Parse HTML into structured tag / text / metadata results
- CSS-style selectors (
#id,.class, tag name) - Whitelist-based sanitization
- Minification with comment stripping
- Link and table extraction
- LRU result caching with TTL
- RSS 2.0 feed parsing
- JSON / CSV export
- CLI tool
- Standard library only, no third-party dependencies
Installation
pip install pafer
Quickstart
from pafer import HTMLParser
html = '<div class="post"><h1>Title</h1><a href="/about">About</a></div>'
p = HTMLParser()
result = p.parse(html)
result['tags'] # list of {name, attributes, content, rows?}
result['metadata'] # {'title': ...}
result['doctype'] # '<!DOCTYPE ...>' or None
p.select(html, '.post') # match by #id, .class or tag name
p.sanitize(html) # keep only allowed tags
p.minify(html) # collapse whitespace, strip comments
p.extract_links(html) # [{'url': ..., 'text': ..., 'attributes': ...}]
Enable the parsed-result cache with HTMLParser(use_cache=True).
Configuration
from pafer import Config, load_config, save_config
cfg = Config({'strip_whitespace': False, 'cache_size': 100})
save_config(cfg, 'my.pkl')
cfg = load_config('my.pkl')
Default settings
| Key | Default | Purpose |
|---|---|---|
parse_tables |
True |
extract table rows while parsing |
strip_whitespace |
True |
collapse whitespace |
encoding |
utf-8 |
decode bytes input |
max_depth |
100 |
maximum element nesting |
default_tag |
div |
fallback tag |
preserve_comments |
False |
keep comments in the tree |
strict_mode |
False |
raise on malformed HTML |
remove_comments |
True |
strip comments when minifying |
allowed_tags |
p, br, b, i, a, ul, ol, li, div, span |
sanitize whitelist |
max_file_size_mb |
10 |
reject oversized input |
cache_size |
50 |
cache capacity |
Caching
pafer.cache.LRUCache is an in-memory LRU cache with TTL expiry:
from pafer.cache import LRUCache
cache = LRUCache(max_size=100, ttl_seconds=300)
cache.set('key', value)
cache.get('key')
RSS feeds
from pafer.feed import FeedParser
items = FeedParser().parse_rss(xml_content)
items[0]['title'] # 'title', 'link', 'description', 'pubDate'
Exporting
from pafer.exporters import DataExporter
DataExporter.to_json(data) # JSON string with 2-space indent
DataExporter.to_csv(rows) # CSV from a list of rows
CLI
python -m pafer.cli parse -f input.html --minify
python -m pafer.cli sanitize -f input.html
Exceptions
HTMLParserError (base), SanitizationError, SelectorSyntaxError, ConfigLoadError.
LICENSE
MIT - Enjoy
Release files for pafer 0.9.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pafer-0.9.7.tar.gz | 10.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pafer-0.9.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.8 kB
Release files / pafer-0.9.7.tar.gz
| Download URL | pafer-0.9.7.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0e2de77d9ce870a80215ba006527e116df3f2bcc2580a8b522ec7a548a2ceeb
|
|
BLAKE2b-256 checksum How to use checksums |
711cb7f044e9fb31e0ae175bc5bd535e676a7ccc62f46cc03ba1cda4e623d459
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / pafer-0.9.7-py3-none-any.whl
| Download URL | pafer-0.9.7-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24f75cec373a004e17dc0f9ce7c0fbc4317ca10e3563a67fb383cc8c7323f049
|
|
BLAKE2b-256 checksum How to use checksums |
5173817984c6d8d7b94ea3b52ca1dd53446bfd5d24a05f27b0a400ecebe70b50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|