xralf
xralf

Reputation: 3662

Decide if direct children of element are certain tags

Is there a way to write this in BeautifulSoup?

for node in soup:
  if node is in ["a", "b", "i", ...]: # node is tag of type `a` or `b` ...
    # we are probably on the text level
    textLevelFlag = true
  else:
    # "we are higher"

Upvotes: 0

Views: 69

Answers (1)

soulcheck
soulcheck

Reputation: 36767

If you want to test if your tag has only text elements you can try this:

if hasattr(node, contents) and len(node.contents) == 1 and isinstance(node.contents[0], NavigableString):
    textLevelFlag = true
else:
    something else

Upvotes: 1

Related Questions