在Python中使用XPath选择器语法可以通过lxml库来实现。以下是一些XPath选择器的示例:
from lxml import etree
tree = etree.parse('example.xml')
nodes = tree.xpath('//*')
nodes = tree.xpath('//book')
nodes = tree.xpath('//book[@category="web"]')
nodes = tree.xpath('//book[@category="web"]/title')
nodes = tree.xpath('//book/title/text()')
nodes = tree.xpath('//book/title/../@category')
nodes = tree.xpath('//book/title/text()')
nodes = tree.xpath('//book/title[position()=1]')
这些是一些XPath选择器语法的示例,通过这些语法可以方便地获取XML或HTML文档中的指定节点。