from lxml import etree
xml ="""
<book>
<id>1</id>
<name>野花遍地香</name>
<price>1.23</price>
<nick>臭豆腐</nick>
<author>
<nick id='10086'>周大强</nick>
<nick id='10010'>周芷若</nick>
<nick class='joy'>周杰伦</nick>
<nick class='jolin'>蔡依林</nick>
<div>
<nick>惹了</nick>
</div>
<span>
<nick>惹了1</nick>
</span>
</author>
<partner>
<nick id='ppc'>胖胖陈</nick>
<nick id='ppbc'>胖胖不陈</nick>
</partner>
</book>
"""
#xpath解析
tree = etree.XML(xml) #加载xml
#result1 = tree.xpath("/book") #/表示层级关系,第一个/是根节点
#result2 = tree.xpath("/book/name")
#result3 = tree.xpath("/book/name/text()") # text()拿文本
#result4 = tree.xpath("/book/author//nick/text()") # //后代
#result5 = tree.xpath("/book/author/*/nick/text()") # * 任意的节点,通配符(会儿)
result6 = tree.xpath("/book//nick/text()")
print(result1)
print(result2)
print(result3)
print(result4)
print(result5)
print(result6
xpath的简单使用,一些小笔记。