加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

【python小随笔】python 解析xml数据的新手大坑>>抓取多重

发布时间:2020-12-20 10:04:03 所属栏目:Python 来源:网络整理
导读:xml文档: GetMatchingProductResult ASIN= " B071LF9R6G " status= Success " .../GetMatchingProductResultGetMatchingProductResult ASIN= B0714BP3H4 B0756FL8R7 " .../GetMatchingProductResultResponseMetadata # 使用minidom解析器打开XML文档 DOMTree
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读