我有以下(简化的)代码,它使用以下源:
<html>
<p>line 1</p>
<div>
<a>line 2</a>
</div>
</html>
soup = BeautifulSoup('<html><p>line 1</p><div><a>line 2</a></div></html>')
ele = soup.find('p').nextSibling
somehow_print_tag_of_ele_here
我想获取 ele 的标签,在本例中为“div”。然而,我似乎只能得到它的 child 的标签。我错过了一些简单的事情吗?我以为我可以做 ele.tag.name,但这是一个异常(exception),因为 tag 是 None。
#Below correctly prints the div element "<div><a>line 2</a></div>"
print ele
#Below prints "None". Printing tag.name is an exception since tag is None
print ele.tag
#Below prints "a", the child of ele
allTags = ele.findAll(True)
for e in allTags:
print e.name
此时,我正在考虑做一些事情,获取 ele 的父级,然后获取父级子级的标签,并计算 ele 有多少个上级 sibling ,倒计时到正确的子级标签。这看起来很荒谬。
最佳答案
ele
已经是一个标签,请尝试执行以下操作:
soup = BeautifulSoup('<html><p>line 1</p><div><a>line 2</a></div></html>')
print(soup.find('p').nextSibling.name)
所以在你的例子中它只是
print(ele.name)
关于tags - BeautifulSoup:获取元素本身的标签名称,而不是其子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8533673/