美丽的汤解析XML文件

时间:2018-05-01 10:48:32

标签: python beautifulsoup

我正在使用Beautiful Soup编写一个简单的Python来解析xml文件中需要的数据。这是我需要它的工作方式,但我有一个问你们,因为我试图谷歌这个但似乎无法找到我想要的东西。

XML字符串示例:

<ProductAttribute MaintenanceType="C" AttributeID="Attachment Type" PADBAttribute="N" RecordNumber="1" LanguageCode="EN">Clamp-On</ProductAttribute>

我需要ProductAttribute中的 AttributeID 。当我写,下面我能够获取值“Clamp-On”,但我需要AttributeID来告诉我Clamp-On正在引用的内容。

attributes[part.find('PartNumber').get_text()] = [x.get_text() for x in part.find_all('ProductAttribute')]

for key, value in attributes.items():
     for v in value:
     print(v)

任何指导都要在负面反馈之前得到赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

这里是你如何使用BeautifulSoup和lxml,

从xml获取标签属性
from bs4 import BeautifulSoup

xml_string = '<ProductAttribute MaintenanceType="C" AttributeID="Attachment Type" PADBAttribute="N" RecordNumber="1" LanguageCode="EN">Clamp-On</ProductAttribute>'

soup = BeautifulSoup(xml_string, 'xml')
tag = soup.ProductAttribute
print(tag['AttributeID'])

此代码打印属性AttributeID

的值

答案 1 :(得分:0)

仅使用lxml库的简单解决方案:

from lxml import etree

xml_string = """<ProductAttribute MaintenanceType="C" AttributeID="Attachment Type" PADBAttribute="N" RecordNumber="1" LanguageCode="EN">Clamp-On</ProductAttribute>"""

xml = etree.XML(xml_string)
print(xml.get("AttributeID"))

<强>输出

Attachment Type