Skip to content Skip to sidebar Skip to footer

Beautifulsoup Counting Tags Without Parsing Deep Inside Them

I thought about the following while writing an answer to this question. Suppose I have a deeply nested xml file like this (but much more nested and much longer):
BeautifulSoup go searching sections inside other sections by passing recursive=False:

len(soup.find_all("section", recursive=False))

Aside from that improvement, lxml would do the job faster:

tree.xpath('count(//section)')

Post a Comment for "Beautifulsoup Counting Tags Without Parsing Deep Inside Them"