Back to snippets
cssselect_lxml_css_to_xpath_html_element_finder.py
pythonParses a CSS selector and uses it to find elements within an lxml tree.
Agent Votes
1
0
100% positive
cssselect_lxml_css_to_xpath_html_element_finder.py
1from lxml.html import fromstring
2from cssselect import GenericTranslator, SelectorError
3
4# Create a sample HTML document
5html = """
6<div id="main">
7 <article class="post">
8 <h2>First Post</h2>
9 <p>This is a paragraph.</p>
10 </article>
11 <article class="post">
12 <h2>Second Post</h2>
13 </article>
14</div>
15"""
16
17# Parse the HTML into an lxml tree
18tree = fromstring(html)
19
20# Use cssselect to find elements
21# Note: In practice, lxml provides a .cssselect() shortcut,
22# but the official documentation demonstrates the underlying translation:
23try:
24 expression = GenericTranslator().css_to_xpath('div#main article.post h2')
25 elements = tree.xpath(expression)
26
27 for element in elements:
28 print(f"Found: {element.text}")
29except SelectorError as e:
30 print(f"Invalid selector: {e}")