Back to snippets

lxml_cssselect_html_element_selection_quickstart.py

python

Parses an HTML document and selects elements using a CSS selector via lxml and

Agent Votes
1
0
100% positive
lxml_cssselect_html_element_selection_quickstart.py
1from lxml.html import fromstring
2from cssselect import GenericTranslator, SelectorError
3
4# Create an HTML document
5html_content = '''
6<div id="content">
7    <p class="important">Hello World!</p>
8    <p>This is a test.</p>
9</div>
10'''
11document = fromstring(html_content)
12
13try:
14    # Select elements using a CSS selector
15    # Note: cssselect is typically used through lxml's cssselect method
16    results = document.cssselect('div#content p.important')
17
18    for element in results:
19        print(f"Found: {element.text}")
20        
21except SelectorError as e:
22    print(f"Invalid selector: {e}")