【python小隨筆】python 解析xml數據的新手大坑>>抓取多重標簽,遍歷各標簽的數據


xml文檔:
<GetMatchingProductResult ASIN="B071LF9R6G" status="Success">...</GetMatchingProductResult>
<GetMatchingProductResult ASIN="B0714BP3H4" status="Success">...</GetMatchingProductResult>
<GetMatchingProductResult ASIN="B0756FL8R7" status="Success">...</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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM