如何获取bsoup xml解析的next_element结果的标签名?

How to get the tag name of the next_element result for bsoup xml parsing?

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

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

现在我要这个元素的标签,就是string。我如何获得它?

s.next_element.tag 不工作。

您可以使用 .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']