Difference between findall() and xpath()

Hi,
I am not sure what is the difference between findall() and xpath(). Could anybody show all the examples in which they are different?
$ cat main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
from StringIO import StringIO from lxml import etree tree = etree.parse(StringIO('<foo><x><bar>abc</bar></x><bar>xyz</bar></foo>'))
r = tree.xpath('/foo') print r[0].findall('bar') print r[0].xpath('bar') $ ./main.py [<Element bar at 0x102b6ce18>] [<Element bar at 0x102b6ce18>]
Also, I see that findall() can be faster. Is it case that when I can use both, I should use findall() instead of xpath()? Thanks.
https://blog.startifact.com/posts/older/lxml-findall-and-xpath-performance.h...

Am 17. März 2018 23:16:34 MEZ schrieb Peng Yu:
I am not sure what is the difference between findall() and xpath(). Could anybody show all the examples in which they are different?
Does this help?
http://lxml.de/tutorial.html#elementpath
Also, I see that findall() can be faster. Is it case that when I can use both, I should use findall() instead of xpath()?
IMHO yes. Or rather, iterate instead of requesting a complete result list whenever you can.
Stefan
participants (2)
-
Peng Yu
-
Stefan Behnel