Leaf
What is this?
This is a simple wrapper around lxml which adds some nice features to make working with lxml better. This library covers all my needs in HTML parsing.
Dependencies
lxml obviously :3
Features
- Nice jquery-like CSS selectors
- Simple access to element attributes
- Easy way to convert HTML to other formats (bbcode, markdown, etc.)
- A few nice functions for working with text
- And, of course, all original features of lxml
Description
The main function of the module (for my purposes) is leaf.parse. This
function takes an HTML string as argument, and returns a leaf.Parser
object, which wraps an lxml object.
With this object you can do anything you want, for example:
document = leaf.parse(sample)
# get the links from the DIV with id 'menu' using CSS selectors
links = document('div#menu a')
Or you can do this:
# get first link or return None
link = document.get('div#menu a')
And you can get attributes from these results like this:
print link.onclick
You can also use standard lxml methods like object.xpath, and they
return results as leaf.Parser objects.
My favorite feature is parsing HTML into bbcode (markdown, etc.):
# Let's define simple formatter, which passes text
# and wraps links into [url][/url] (like bbcode)
def code_formatter(element, children):
# Replace <br> tag with line break
if element.tag == 'br':
return '\n'
# Wrap links into [url][/url]
if element.tag == 'a':
return u"[url=link}]{text}[/url]".format(link=element.href, text=children)
# Return children only for other elements.
if children:
return children
This function will be recursively called with element and children (this is string with children parsing result).
So, let's call this parser on some leaf.Parser object:
document.parse(code_formatter)
More detailed examples available in the tests.
Finally, this library has some nice functions for working with text:
| Name | Description |
|---|---|
| to_unicode | Convert string to unicode string |
| strip_accents | Strip accents from a string |
| strip_symbols | Strip ugly unicode symbols from a string |
| strip_spaces | Strip excess spaces from a string |
| strip_linebreaks | Strip excess line breaks from a string |
Change log
1.0.7
- Fix badges in README.md
- cleanup CHANGES.md
1.0.6
- Fix installation script on LICENSE file
1.0.4
- Convert documentation to Markdown
- Add support for universal wheel
1.0.1
- 100% test coverage
- fixed bug in result wrapping (etree._Element has __iter__ too!)
1.0
- add python3 support
- first production release
0.4.4
- fix inner_html method
- added **kwargs to the parse function, added inner_html method to the Parser class
- cssselect in deps
0.4.2
- Node attribute modification via node.href = '/blah'
- Custom default value for get: document.get(selector, default=None)
- Get element by index: document.get(selector, index)
0.4.1
- bool(node) returns True if element exists and False if element is None
0.4
- First public version
Metadata
Release files for leaf 1.0.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 | Provenance | Actions |
|---|---|---|---|---|
| leaf-1.0.7.tar.gz | 5.8 kB | Without provenance | Details for leaf-1.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| leaf-1.0.7-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for leaf-1.0.7-py2.py3-none-any.whl |
Total release size: 11.6 kB
Release files / leaf-1.0.7.tar.gz
| Download URL | leaf-1.0.7.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38c7fdef9de1a67961794d981260cd2dc5c16bb705aa11c746565f9b52856aa9
|
|
BLAKE2b-256 checksum How to use checksums |
18a45c8c5caac9e03ea33b2384d16f5167c474cd7194cb2d7718de1d4d6156c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.4
|
Release files / leaf-1.0.7-py2.py3-none-any.whl
| Download URL | leaf-1.0.7-py2.py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
d3ea38bf05e1cb4caee373192fc30c53a09c7890f2a000baf7b473df0a989910
|
|
BLAKE2b-256 checksum How to use checksums |
0105dc58afe5bd51f3016a1329f7e891f77daf5b63abe518643be1b8cd9c4623
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.4
|
Security for leaf 1.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.