### Defining Example HTML and Fragment Strings Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Initializes multi-line string variables `html` and `fragment` containing example HTML structures. These strings are used throughout the tutorial for parsing and manipulation demonstrations. ```python html = """

Welcome to selectolax tutorial

Lorem ipsum

Lorem ipsum dolor sit amet, ea quo modus meliore platonem.

""" fragment = """

Hello there!

""" ``` -------------------------------- ### Install selectolax development version from GitHub Source: https://github.com/rushter/selectolax/blob/master/README.rst Steps to clone and install the latest development version of selectolax directly from its GitHub repository, including dependency installation. ```bash git clone --recursive https://github.com/rushter/selectolax cd selectolax pip install -r requirements_dev.txt python setup.py install ``` -------------------------------- ### Install selectolax via pip Source: https://github.com/rushter/selectolax/blob/master/README.rst Instructions for installing the selectolax library using pip. This is the standard method for stable releases. ```bash pip install selectolax ``` -------------------------------- ### Preparing HTML for Encoding Detection Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Sets up an example HTML string containing non-ASCII characters and encodes it into `cp1251` bytes. This byte string is then used to demonstrate Selectolax's encoding detection capabilities. ```python html = "
Привет мир!
" # Encoding detector works only with raw strings (bytes) html_bytes = html.encode('cp1251') ``` -------------------------------- ### Install selectolax with Cython for compilation issues Source: https://github.com/rushter/selectolax/blob/master/README.rst Provides a solution for selectolax installation failures due to compilation errors, typically by installing Cython alongside selectolax. This is often needed for older selectolax versions on newer Python. ```bash pip install selectolax[cython] ``` -------------------------------- ### Inserting Nodes in Selectolax HTML Tree Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Illustrates various methods for inserting content (plain text or other HTML nodes) relative to an existing node within a `selectolax` HTML tree. Examples include `insert_before` to add content before a node, `insert_after` to add content after a node, and `insert_child` to append content inside a node. ```python html = """
""" tree = HTMLParser(html) # Insert text dest_node = html_parser.css_first('.red') dest_node.insert_before("Hello") # Insert nodes subtree = HTMLParser("
Hi
") dest_node = html_parser.css_first('.red') dest_node.insert_before(subtree) # Insert before, after, or append inside subtree = HTMLParser("
Car
") dest_node = html_parser.css_first('.green') dest_node.insert_before(subtree) dest_node.insert_after(subtree) dest_node.insert_child(subtree) ``` -------------------------------- ### Importing Selectolax Parsing Functions Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Imports the core classes and functions from the `selectolax.parser` module, including `HTMLParser` for full document parsing, `parse_fragment` for HTML fragments, and `create_tag` for single node creation. ```python from selectolax.parser import HTMLParser, parse_fragment, create_tag ``` -------------------------------- ### Displaying Original HTML Content Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Prints the original HTML string defined earlier, serving as a reference point before demonstrating tag unwrapping operations. ```python print(html) ``` -------------------------------- ### Traversing the Entire HTML Document Tree Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Shows how to perform a full traversal of the HTML document tree starting from the root. It iterates through every node, identifying text nodes (`-text`) to print their stripped text content, and printing the tag name for other element nodes. ```python html_parser = HTMLParser(html) for node in html_parser.root.traverse(): if node.tag == '-text': text = node.text(deep=True).strip() if text: print(text) else: print(node.tag) ``` -------------------------------- ### Parsing HTML Documents and Fragments with Selectolax Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates the three primary ways to parse HTML using Selectolax: `HTMLParser` for full HTML documents, `parse_fragment` for HTML partials, and `create_tag` for generating a new, empty HTML tag. ```python html_tree = HTMLParser(html) frag_tree = parse_fragment(fragment) node = create_tag("div") ``` -------------------------------- ### Displaying Encoded HTML Bytes Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Outputs the raw byte string of the previously encoded HTML, showing its representation in `cp1251` encoding. ```python html_bytes ``` -------------------------------- ### Detecting HTML Encoding from Byte String Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates how Selectolax automatically detects the encoding of an HTML document when provided as a byte string. By setting `detect_encoding=True`, the `input_encoding` property reveals the detected character set. ```python HTMLParser(html_bytes, detect_encoding=True).input_encoding ``` -------------------------------- ### Unwrapping Tags from HTML Document Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Illustrates the `unwrap_tags()` method, which removes the specified tags (`p`, `i`) but keeps their content. This effectively 'lifts' the content out of the tags, printing the modified body HTML. ```python html_parser = HTMLParser(html) html_parser.unwrap_tags(['p', 'i']) print(html_parser.body.html) ``` -------------------------------- ### Selecting the First Matching Element by CSS Selector Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Shows how to retrieve only the first element that matches a specified CSS selector (`h1`) using `HTMLParser().css_first()`. The text content of the found element is then printed. ```python print("H1: %s" % HTMLParser(html).css_first('h1').text()) ``` -------------------------------- ### Handling No Matches with Default Value in Selectolax Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates the use of the `default` parameter with `css_first()`. If no element matches the selector (`title` in this case), the specified default value ('not-found') is returned instead of raising an error. ```python print("Title: %s" % HTMLParser(html).css_first('title', default='not-found')) ``` -------------------------------- ### Chaining CSS Selectors with Selectolax Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates how to chain multiple CSS selectors using the `selectolax` library to progressively filter HTML elements. Each subsequent `.css()` call refines the selection based on the results of the previous selector, allowing for precise targeting of nested elements. ```python html = """
""" tree = HTMLParser(html) print([node.html for node in tree.select('div').css("span").css(".red").matches]) ``` -------------------------------- ### Python Project Dependency List Source: https://github.com/rushter/selectolax/blob/master/requirements_dev.txt Lists the required Python packages and their minimum or exact version constraints for the project. This file typically serves as a `requirements.txt` or similar dependency specification for development, testing, and documentation environments, ensuring consistent environments across different setups. ```Python pip>=18.0 bumpversion>=0.5.3 wheel>=0.29.0 watchdog>=0.8.3 tox>=2.3.1 coverage>=4.1 Sphinx==8.0.2 numpydoc==1.8.0 pytest>=3.7.2 pytest-runner>=4.2 Cython>=3.0.11 pluggy>=0.7.1 mypy==1.4.1 types-pyinstaller==6.10.0.20240812 furo==2024.8.6 sphinxext-opengraph==0.9.1 sphinx-copybutton==0.5.2 ruff setuptools>=75.7.0 pytest-mypy-plugins>=3.2,<4.0.0 ``` -------------------------------- ### Applying Nested CSS Selectors for Granular Selection Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates how to chain `css_first()` calls to apply nested selectors. This allows for more precise selection by first finding a parent element (`div#text`) and then searching for a child element (`p:nth-child(2)`) within its scope. ```python HTMLParser(html).css_first('div#text').css_first('p:nth-child(2)').html ``` -------------------------------- ### Selecting All Elements by CSS Selector in Selectolax Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Illustrates how to select all elements matching a given CSS selector (`p.p3`) from an HTML document using `HTMLParser().css()`. It then iterates through the matched nodes, printing various properties like HTML content, attributes, text, tag name, and parent tag. ```python selector = "p.p3" for node in HTMLParser(html).css(selector): print('---------------------') print('Node: %s' % node.html) print('attributes: %s' % node.attributes) print('node text: %s' % node.text(deep=True, separator='', strip=False)) print('tag: %s' % node.tag) print('parent tag: %s' % node.parent.tag) if node.last_child: print('last child inside current node: %s' % node.last_child.html) print('---------------------\n') ``` -------------------------------- ### Accessing Parent Node of a Selected Element Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Illustrates how to navigate the DOM tree by accessing the parent node of a selected element. It selects the paragraph with `id='stext'` and then prints the full HTML content of its parent element. ```python print(HTMLParser(html).css_first('p#stext').parent.html) ``` -------------------------------- ### Detecting Encoding Using Meta Tags (UTF-8) Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Further demonstrates encoding detection with meta tags, this time using a `WINDOWS-1251` meta tag but encoding the HTML into `utf-8` bytes. Selectolax will still prioritize the `meta charset` value when `use_meta_tags=True`. ```python html_utf = ''.encode('utf-8') HTMLParser(html_utf, detect_encoding=True, use_meta_tags=True).input_encoding ``` -------------------------------- ### Removing Tags from HTML Document Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates how to remove specific tags from an HTML document. It selects all 'p' tags using `tags('p')` and then calls `decompose()` on each node to remove it from the HTML tree, printing the modified body HTML. ```python html_parser = HTMLParser(html) for node in html_parser.tags('p'): node.decompose() print(html_parser.body.html) ``` -------------------------------- ### Enforcing Strict Single Match Selection in Selectolax Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Explains how to use the `strict=True` parameter with `css_first()`. This ensures that if more than one element matches the selector (`p.p3`), an error will be raised, enforcing that only a single unique match is expected. ```python HTMLParser(html).css_first("p.p3", default='not-found', strict=True) ``` -------------------------------- ### Iterating Over Child Nodes at the Current Level Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Shows how to iterate over all direct child nodes of a selected element. It first selects `div#text` and then uses `node.iter()` to traverse and print the tag name and HTML content of each immediate child node. ```python for node in HTMLParser(html).css("div#text"): for cnode in node.iter(): print(cnode.tag, cnode.html) ``` -------------------------------- ### Detecting Encoding Using Meta Tags (CP1251) Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Illustrates encoding detection when a `meta charset` tag is present in the HTML. It encodes an HTML string with a `WINDOWS-1251` meta tag into `cp1251` bytes and shows how Selectolax correctly identifies the encoding by prioritizing the meta tag with `use_meta_tags=True`. ```python html = ''.encode('cp1251') HTMLParser(html, detect_encoding=True, use_meta_tags=True).input_encoding ``` -------------------------------- ### Using Advanced Selectors with Text Content Filtering Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Introduces advanced selector capabilities, mimicking XPath-like features. It selects all ` """ tree = HTMLParser(html) [node.text() for node in tree.select('script').text_contains("super").matches] ``` -------------------------------- ### Manipulating Element Attributes in Selectolax Source: https://github.com/rushter/selectolax/blob/master/examples/walkthrough.ipynb Demonstrates how to access, modify, add, and delete attributes of a selected HTML element. It selects `div#text`, adds a new 'data' attribute, modifies its 'id' attribute, and then deletes the 'id' attribute, printing the attributes dictionary and the node's HTML at various stages. ```python html_parser = HTMLParser(html) node = html_parser.css_first('div#text') node.attrs['data'] = 'secrect data' node.attrs['id'] = 'new_id' print(node.attributes) del node.attrs['id'] print(node.attributes) print(node.html) ``` -------------------------------- ### Compile selectolax for development Source: https://github.com/rushter/selectolax/blob/master/README.rst Commands to clean and recompile selectolax during development, useful for testing changes to the C backend. ```bash make clean make dev ``` -------------------------------- ### Parse HTML and extract text/attributes with selectolax HTMLParser Source: https://github.com/rushter/selectolax/blob/master/README.rst Demonstrates basic HTML parsing using `selectolax.parser.HTMLParser` to create a parse tree. Shows how to extract text from a specific element using `css_first().text()` and retrieve all attributes using `.attributes`. Also illustrates extracting text from multiple elements matching a CSS selector. ```python from selectolax.parser import HTMLParser html = """

Hi there

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
""" tree = HTMLParser(html) # tree.css_first('h1#title').text() # Out[2]: 'Hi there' # tree.css_first('h1#title').attributes # Out[3]: {'id': 'title', 'data-updated': '20201101'} # [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.'] ``` -------------------------------- ### Use LexborHTMLParser backend in selectolax Source: https://github.com/rushter/selectolax/blob/master/README.rst Demonstrates how to explicitly use the `LexborHTMLParser` backend, which is the preferred and actively maintained option in selectolax. It shows parsing HTML and extracting text from an element using CSS selectors, similar to the default `HTMLParser`. ```python from selectolax.lexbor import LexborHTMLParser html = """ Hi there
2021-08-15
""" parser = LexborHTMLParser(html) parser.root.css_first("#updated").text() # Out[4]: '2021-08-15' ``` -------------------------------- ### Apply advanced CSS selectors with selectolax Source: https://github.com/rushter/selectolax/blob/master/README.rst Illustrates the use of complex CSS selectors, including `nth-child` and `:not(:has())`, to filter HTML nodes. It demonstrates iterating through selected nodes and printing their attributes, text content, tag name, parent tag, and full HTML. ```python html = "

link

text

" selector = "div > :nth-child(2n+1):not(:has(a))" for node in HTMLParser(html).css(selector): print(node.attributes, node.text(), node.tag) print(node.parent.tag) print(node.html) # Output: # {'id': 'p1'} p # div #

# {'id': 'p5'} text p # div #

text

``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.