如何使用 BeautifulSoup4 获取名为 "name" 的 XML 元素的字符串值?

How can I get the string value of a XML element named "name" with BeatifulSoup4?

当我尝试用 BeatifulSoup4

解析 XML 元素(标签)“名称”时
exemplary_xml = '''
<SomeTag>
    <UsualTag>abc</UsualTag>
    <Name>xyz</Name>
</SomeTag>
'''

soup = BeautifulSoup(exemplary_xml, parser="xml")
print(soup.sometag.usualtag.string)
print(soup.sometag.name.string)

我收到一个错误,因为它与 API .name for accessing the tags name:

冲突
abc
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [16], in <module>
      8 soup = BeautifulSoup(exemplary_xml, parser="lxml")
      9 print(soup.sometag.usualtag.string)
---> 10 print(soup.sometag.name.string)

AttributeError: 'str' object has no attribute 'string'

如何获取名为“name”的 XML element/tag 的字符串值?

您通过 bs4 使用 xml 的方式很奇怪且已弃用。使用 features 然后 find()find_all().

例如:

from bs4 import BeautifulSoup

exemplary_xml = '''
<SomeTag>
    <UsualTag>abc</UsualTag>
    <Name>xyz</Name>
</SomeTag>
'''

soup = BeautifulSoup(exemplary_xml, features="xml")
print(soup.find("UsualTag").string)
print(soup.find("Name").string)

输出:

abc
xyz