有没有办法在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"
答案 0 :(得分:1)
如果您想测试您的代码是否只有文字元素,可以试试这个:
if hasattr(node, contents) and len(node.contents) == 1 and isinstance(node.contents[0], NavigableString):
textLevelFlag = true
else:
something else