代码:
from lxml import etree
html = etree.parse('hello.html')
print type(html)
result = html.xpath('//li')
print result
print len(result)
print type(result)
print type(result[0])运行结果
[ , , , , ] 5
可见,etree.parse 的类型是 ElementTree,通过调用 xpath 以后,得到了一个列表,包含了 5 个
元素,每个元素都是 Element 类型 (2)获取
标签的所有 class
result = html.xpath('//li/@class')
print result运行结果
['item-0', 'item-1', 'item-inactive', 'item-1', 'item-0']