A fast HTML5 parser with CSS selectors, written in Cython, using the Lexbor engine.
Actively maintained. Last release 6 weeks ago, commits in 12 of the last 26 weeks. A safe choice.
pip install selectolax
A fast HTML5 parser with CSS selectors, written in Cython, using the Lexbor engine.
Installation
From PyPI using pip:
pip install selectolax
If installation fails due to compilation errors, you may need to install Cython:
pip install selectolax[cython]
This usually happens when you try to install an outdated version of selectolax on a newer version of Python.
Development version from GitHub:
git clone --recursive https://github.com/rushter/selectolax
cd selectolax
pip install -r requirements_dev.txt
python setup.py install
How to compile selectolax while developing:
make clean
make dev
Basic examples
Here are some basic examples to get you started with selectolax:
Parsing HTML and extracting text:
from selectolax.lexbor import LexborHTMLParser
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 = LexborHTMLParser(html)
print(tree.css_first('h1#title').text())
# 'Hi there'
print(tree.css_first('h1#title').attributes)
# {'id': 'title', 'data-updated': '20201101'}
print([node.text() for node in tree.css('.post')])
# ['Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
# 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.']