[lxml-dev] Efficient way to search element attribs?
Hi all, When building a tree, I add a particular attribute to each element (eg: ID), and was wondering if there are any methods I could use to search the tree for tags that have this attribute? I looked at find(), findall() and the xpath functions - but they seem to help only when you know the tag name itself. Of course, one way might be to do a manual depth first or breadth first search from the root downwards, recursively.. But that would become expensive pretty soon. Thanks, Viksit
Hi, Viksit Gaur wrote:
When building a tree, I add a particular attribute to each element (eg: ID), and was wondering if there are any methods I could use to search the tree for tags that have this attribute?
I looked at find(), findall() and the xpath functions - but they seem to help only when you know the tag name itself.
You should read a bit on XPath, or at least on the ElementPath syntax. http://effbot.org/zone/element-xpath.htm The '*' wildcard allows you to search for any element tag. All iterators in lxml.etree support it, BTW. Stefan
participants (2)
-
Stefan Behnel
-
Viksit Gaur