Skip to main content

Fast HTML5 parser with CSS selectors.

Project description

selectolax logo
https://img.shields.io/pypi/v/selectolax.svg

A fast HTML5 parser with CSS selectors using Modest and Lexbor engines.

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

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>

Available backends

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.

To use lexbor, just import the parser and use it in the similar way to the HTMLParser.

In [1]: from selectolax.lexbor import LexborHTMLParser

In [2]: html = """
   ...: <title>Hi there</title>
   ...: <div id="updated">2021-08-15</div>
   ...: """

In [3]: parser = LexborHTMLParser(html)
In [4]: parser.root.css_first("#updated").text()
Out[4]: '2021-08-15'

Simple Benchmark

  • 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)

61.02 sec.

lxml

9.09 sec.

html5_parser

16.10 sec.

selectolax (Modest)

2.94 sec.

selectolax (Lexbor)

2.39 sec.

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

selectolax-0.3.24.tar.gz (3.6 MB view hashes)

Uploaded Source

Built Distributions

selectolax-0.3.24-cp313-cp313-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.13 Windows x86-64

selectolax-0.3.24-cp313-cp313-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.13 Windows x86

selectolax-0.3.24-cp313-cp313-musllinux_1_2_x86_64.whl (7.3 MB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp313-cp313-musllinux_1_2_i686.whl (6.7 MB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ i686

selectolax-0.3.24-cp313-cp313-musllinux_1_2_aarch64.whl (7.2 MB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ ARM64

selectolax-0.3.24-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.3 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ARM64

selectolax-0.3.24-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.8 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp313-cp313-macosx_10_13_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.13 macOS 10.13+ x86-64

selectolax-0.3.24-cp313-cp313-macosx_10_13_universal2.whl (5.7 MB view hashes)

Uploaded CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64)

selectolax-0.3.24-cp312-cp312-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

selectolax-0.3.24-cp312-cp312-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.12 Windows x86

selectolax-0.3.24-cp312-cp312-musllinux_1_2_x86_64.whl (7.3 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp312-cp312-musllinux_1_2_i686.whl (6.8 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ i686

selectolax-0.3.24-cp312-cp312-musllinux_1_2_aarch64.whl (7.2 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

selectolax-0.3.24-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

selectolax-0.3.24-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.8 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp312-cp312-macosx_10_13_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.12 macOS 10.13+ x86-64

selectolax-0.3.24-cp312-cp312-macosx_10_13_universal2.whl (5.7 MB view hashes)

Uploaded CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64)

selectolax-0.3.24-cp311-cp311-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

selectolax-0.3.24-cp311-cp311-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.11 Windows x86

selectolax-0.3.24-cp311-cp311-musllinux_1_2_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp311-cp311-musllinux_1_2_i686.whl (6.7 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ i686

selectolax-0.3.24-cp311-cp311-musllinux_1_2_aarch64.whl (7.1 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

selectolax-0.3.24-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

selectolax-0.3.24-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp311-cp311-macosx_10_9_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

selectolax-0.3.24-cp311-cp311-macosx_10_9_universal2.whl (5.7 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

selectolax-0.3.24-cp310-cp310-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

selectolax-0.3.24-cp310-cp310-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.10 Windows x86

selectolax-0.3.24-cp310-cp310-musllinux_1_2_x86_64.whl (7.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp310-cp310-musllinux_1_2_i686.whl (6.5 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ i686

selectolax-0.3.24-cp310-cp310-musllinux_1_2_aarch64.whl (7.0 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

selectolax-0.3.24-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

selectolax-0.3.24-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp310-cp310-macosx_10_9_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

selectolax-0.3.24-cp310-cp310-macosx_10_9_universal2.whl (5.7 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

selectolax-0.3.24-cp39-cp39-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

selectolax-0.3.24-cp39-cp39-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.9 Windows x86

selectolax-0.3.24-cp39-cp39-musllinux_1_2_x86_64.whl (7.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp39-cp39-musllinux_1_2_i686.whl (6.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ i686

selectolax-0.3.24-cp39-cp39-musllinux_1_2_aarch64.whl (7.0 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

selectolax-0.3.24-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

selectolax-0.3.24-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp39-cp39-macosx_10_9_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

selectolax-0.3.24-cp39-cp39-macosx_10_9_universal2.whl (5.7 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

selectolax-0.3.24-cp38-cp38-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

selectolax-0.3.24-cp38-cp38-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.8 Windows x86

selectolax-0.3.24-cp38-cp38-musllinux_1_2_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp38-cp38-musllinux_1_2_i686.whl (6.7 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ i686

selectolax-0.3.24-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp38-cp38-macosx_10_9_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

selectolax-0.3.24-cp38-cp38-macosx_10_9_universal2.whl (5.7 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

selectolax-0.3.24-cp37-cp37m-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

selectolax-0.3.24-cp37-cp37m-win32.whl (2.3 MB view hashes)

Uploaded CPython 3.7m Windows x86

selectolax-0.3.24-cp37-cp37m-musllinux_1_2_x86_64.whl (6.9 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ x86-64

selectolax-0.3.24-cp37-cp37m-musllinux_1_2_i686.whl (6.4 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ i686

selectolax-0.3.24-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.0 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

selectolax-0.3.24-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (6.4 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

selectolax-0.3.24-cp37-cp37m-macosx_10_9_x86_64.whl (3.1 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page