tags - BeautifulSoup:获取元素本身的标签名称,而不是其子元素

标签 tags beautifulsoup

我有以下(简化的)代码,它使用以下源:

<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/

相关文章:

python - 使用 BeautifulSoup 从 investing.com 抓取 BTC/ETH 数据

python - BeautifulSoup find_all() 未找到所有请求的元素

python - Django 多标签字段

<img title ="<a href=' #' onClick=' alert ('Hello World!' )>The Link</a>"/> 中的 JavaScript 可能吗?

javascript - 满足条件时进行网页抓取

web-scraping - 如何限制BeautifulSoup找到的元素数量?

ios - 如何获取带有标签的 UISwitch?

html - A 标签/img 标签后的额外间距?

python - 导入错误 : no module named html. 解析器

python - BeautifulSoup: AttributeError: 'NavigableString' 对象没有属性 'name'