【python小随笔】python 解析xml数据的新手大坑>>抓取多重标签,遍历各标签的数据

前端之家收集整理的这篇文章主要介绍了【python小随笔】python 解析xml数据的新手大坑>>抓取多重标签,遍历各标签的数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
xml文档:
<GetMatchingProductResult ASIN="B071LF9R6G" status=Success">...</GetMatchingProductResult>
<GetMatchingProductResult ASIN=B0714BP3H4B0756FL8R7">...</GetMatchingProductResult>
<ResponseMetadata>

# 使用minidom解析器打开XML文档
DOMTree = xml.dom.minidom.parse(./test.xml")
collection = DOMTree.documentElement

 collection是针对整个文件的文档,GetMatchingProductResults 获取的是三个名字相同的标签,不同标签的数据对象
GetMatchingProductResults = collection.getElementsByTagName('GetMatchingProductResult')

 GetMatching 是针对每个标签对象GetMatching作为xml对象进行对应取值
for GetMatching in GetMatchingProductResults:
    asin = GetMatching.getElementsByTagName(ASIN)[0].childNodes[0].data
    print(asin)

猜你在找的Python相关文章