<jupyter_start><jupyter_text>Select all p tags with class p3<jupyter_code>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')<jupyter_output>---------------------
Node: <p class="p3" style="display:none;">Excepteur sint occaecat cupidatat non proident</p>
attributes: {'class': 'p3', 'style': 'display:none;'}
node text: Excepteur sint occaecat cupidatat non proident
tag: p
parent tag: div
last child inside current node: Excepteur sint occaecat cupidatat non proident
---------------------

---------------------
Node: <p class="p3" vid="">Lorem ipsum</p>
attributes: {'class': 'p3', 'vid': None}
node text: Lorem ipsum
tag: p
parent tag: div
last child inside current node: Lorem ipsum
---------------------<jupyter_text>Select first match<jupyter_code>print("H1: %s" % HTMLParser(html).css_first('h1').text())<jupyter_output>H1: Welcome to selectolax tutorial<jupyter_text>Default return value if there is no matches<jupyter_code>print("Title: %s" % HTMLParser(html).css_first('title', default='not-found'))<jupyter_output>Title: not-found<jupyter_text>Strictly one match<jupyter_code>HTMLParser(html).css_first("p.p3", default='not-found', strict=True)<jupyter_output><empty_output><jupyter_text>Print parent of pstext<jupyter_code>print(HTMLParser(html).css_first('p#stext').parent.html)<jupyter_output><div>
        <p id="stext">Lorem ipsum dolor sit amet, ea quo modus meliore platonem.</p>
    </div><jupyter_text>Nested selectors<jupyter_code>HTMLParser(html).css_first('div#text').css_first('p:nth-child(2)').html<jupyter_output><empty_output><jupyter_text>Iterate over all nodes on the current level<jupyter_code>for node in HTMLParser(html).css("div#text"):
    for cnode in node.iter():
        print(cnode.tag, cnode.html)<jupyter_output>p <p class="p3" style="display:none;">Excepteur sint occaecat cupidatat non proident</p>
p <p class="p3" vid="">Lorem ipsum</p><jupyter_text>Tag removal<jupyter_code>html_parser = HTMLParser(html)
for node in html_parser.tags('p'):
    node.decompose()
print(html_parser.body.html)<jupyter_output><body>
    <span id="vspan"></span>
    <h1>Welcome to selectolax tutorial</h1>
    <div id="text">
        
        
    </div>
    <div>
        
    </div>

</body><jupyter_text>Encoding detection<jupyter_code>html = "<div>Привет мир!</div>"
# Encoding detector works only with raw strings (bytes)
html_bytes = html.encode('cp1251')
html_bytes
HTMLParser(html_bytes, detect_encoding=True).input_encoding<jupyter_output><empty_output><jupyter_text>Encoding detection using meta tags<jupyter_code>html = '<head><meta charset="WINDOWS-1251"></head>'.encode('cp1251')
HTMLParser(html, detect_encoding=True, use_meta_tags=True).input_encoding
html_utf = '<head><meta charset="WINDOWS-1251"></head>'.encode('utf-8')
HTMLParser(html_utf, detect_encoding=True, use_meta_tags=True).input_encoding<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18309002433090024, 'fraction_numerical': 0.009732360097323601, 'mean_word_length': 4.98, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '47124', 'license': 'MIT', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1153, 'n_words': 226, 'path': 'examples/walkthrough.ipynb', 'repo_name': 'gugaevkirill/selectolax', 'size': '3288'}