如何在随机lxml.html.Element(与root相对)的上下文中使用xpath()?

时间:2012-05-17 18:50:42

标签: python xpath lxml

stu @ sente~ $ ipython

In [1]: import lxml.html

In [2]: root = lxml.html.parse("http://docs.python.org/").getroot()

In [3]: print "there are %d links in the document" % len(root.xpath('//a'))
there are 40 links in the document

In [4]: for table in root.xpath('//table'):
   ...:     print "there are %d links found" % len(table.xpath('//a'))
   ...:
there are 40 links found
there are 40 links found
there are 40 links found

In [5]: print "but there are only %d links within the three tables" % len(root.xpath('//table//a'))
but there are only 21 links within the three tables

In [6]: # can I somehow get the 'table.xpath()' which starts the xpath() query at the table node?

In [7]:

1 个答案:

答案 0 :(得分:2)

在XPath中,.表示上下文元素:

>>> for table in root.xpath('//table'):
...     print "%d links in this table" % len(table.xpath(".//a"))
...     
12 links in this table
5 links in this table
4 links in this table