在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文檔中的指定節點。