A fast HTML5 parser with CSS selectors using Modest and Lexbor engines.
From PyPI using pip:
Development version from GitHub:
How to compile selectolax while developing:
In [1]: from selectolax.parser import HTMLParser
...:
...: html = """
...: <h1 id="title" data-updated="20201101">Hi there</h1>
...: <div class="post">Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div>
...: <div class="post">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
...: """
...: tree = HTMLParser(html)
In [2]: tree.css_first('h1#title').text()
Out[2]: 'Hi there'
In [3]: tree.css_first('h1#title').attributes
Out[3]: {'id': 'title', 'data-updated': '20201101'}
In [4]: [node.text() for node in tree.css('.post')]
Out[4]:
['Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.']
In [1]: html = "<div><p id=p1><p id=p2><p id=p3><a>link</a><p id=p4><p id=p5>text<p id=p6></div>"
...: selector = "div > :nth-child(2n+1):not(:has(a))"
In [2]: for node in HTMLParser(html).css(selector):
...: print(node.attributes, node.text(), node.tag)
...: print(node.parent.tag)
...: print(node.html)
...:
{'id': 'p1'} p
div
<p id="p1"></p>
{'id': 'p5'} text p
div
<p id="p5">text</p>
Selectolax supports two backends: Modest
and Lexbor
. By default, all examples use the Modest backend. Most of the features between backends are almost identical, but there are still some differences.
Currently, the Lexbor
backend is in beta and missing some of the features.
To use lexbor
, just import the parser and use it in the similar way to the HTMLParser.
- Extract title, links, scripts and a meta tag from main pages of top 754 domains. See
examples/benchmark.py
for more information.
Package | Time |
---|---|
Beautiful Soup (html.parser) |
|
lxml |
|
html5_parser |
|
selectolax (Modest) |
|
selectolax (Lexbor) |
|