首页 > 解决方案 > bsoup xml解析如何获取next_element结果的标签名?

问题描述

我有一个 xml,我得到的结果s.next_element

<string content="," height="10" hpos="235" style="bold" vpos="3129" width="4"></string>

现在我想要这个元素的标签,即string. 我如何得到它?

s.next_element.tag不管用。

标签: python-3.xbeautifulsoupxml-parsing

解决方案


您可以使用.name

s.next_elemen.name

from bs4 import BeautifulSoup

soup = BeautifulSoup("""<string content="," height="10" hpos="235" style="bold" vpos="3129" width="4"></string>""", "html.parser")
print([x.name for x in soup]) # Output: ['string']

推荐阅读