Reputation: 67
With BeautifulSoup, is there a property for each Tag
or NavigableString
where we can get the depth or level of the Tag, for instance? like HTML would be level 0 and head
and body
would be 1, and the title
or first div
would be 2, and so forth. would really help ground the computations if that were a property. and the depth property would also be automatically updated if the tree were modified, like by wrap or unwrap, etc.
if the depth property is there and I'm not aware of it, please share. if not, please add this property to a future version.
Upvotes: 1
Views: 1102
Reputation: 1845
As far as I know BeautifulSoup does not have a depth count, or a way to pull out all elements of that depth. If you want to make a request for future versions, you can visit the dicussion group (Which it looks like you have already done.)
Better yet, you can contribute to the source code, or implement your own function to calculate the depth. I recommend starting with this post that implements a count for the depth.
Upvotes: 2