1

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]:
4

1 回答 1

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
于 2012-05-17T18:53:28.150 回答