from lxml import etree html = ''' <html> <body> <p>Hello, World!</p> <div> <p>lxml tutorial</p> <a href="https://www.example.com">example.com</a> </div> </body> </html> ''' tree = etree.HTML(html) elements = tree.cssselect('p')
elements
will contain all elements with the <p>
tag. elements = tree.cssselect('.example')
.example
will select all classes An element named example
. element = tree.cssselect('#main') ```` 在上面的示例中,`#main`将选择ID为`main`的元素。 XPath选择器 lxml库还支持XPath选择器,它是一种使用路径表达式语法来选择元素的方法。以下是一些常用的XPath选择器示例:
elements = tree.xpath('//p')
elements
will contain all <p>
elements. elements = tree.xpath('//a[@href="https://www.example.com"]')
elements
will select all elements with href
The element with the <a>
tag whose attribute value is https://www.example.com
. element = tree.xpath('//p[contains(text(), "lxml tutorial")]')
element
will select elements containing text content as "lxml tutorial "
" element of the <p>
tag. 在上面的示例中,`elements`将选择所有在`<div>`元素下的子孙`<p>`元素。 总结
The above is the detailed content of Don't Miss Guide: Understanding the Features Supported by lxml Selectors. For more information, please follow other related articles on the PHP Chinese website!