首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python解析xml.etree :空结果

使用Python解析xml.etree :空结果
EN

Stack Overflow用户
提问于 2019-09-25 06:45:58
回答 1查看 170关注 0票数 1

我正在尝试用xml.etree解析下面的xml文件。但是,它不会产生任何结果。

代码语言:javascript
复制
from xml.etree import cElementTree as ET
xmlstr = """<?xml version='1.0' encoding='UTF-8'?>

<tns:getCamerasResponse xmlns:tns="https://infoconnect.highwayinfo.govt.nz/schemas/camera2">

    <tns:camera>

        <tns:description>North along Sth Wstn Mwy from May Rd</tns:description>

        <tns:direction>Northbound</tns:direction>

        <tns:group>NA</tns:group>

        <tns:id>653</tns:id>

        <tns:imageUrl>http://www.trafficnz.info/camera/653.jpg</tns:imageUrl>

        <tns:lat>-36.90943</tns:lat>

        <tns:lon>174.73442</tns:lon>

        <tns:name>SH20 May Rd Overbridge</tns:name>

        <tns:offline>false</tns:offline>

        <tns:region>Auckland</tns:region>

        <tns:thumbUrl>http://www.trafficnz.info/camera/thumb/653.jpg</tns:thumbUrl>

        <tns:underMaintenance>false</tns:underMaintenance>

        <tns:viewUrl>http://www.trafficnz.info/camera/view/653</tns:viewUrl>

 </tns:camera>
 </tns:getCamerasResponse>"""



root = ET.fromstring(xmlstr)


results = root.findall('tns:camera', {'tns':"https://infoconnect.highwayinfo.govt.nz/schemas/camera2"})
for camera in results:
    region = camera.find('tns:region')
    if region is not None:
        print(region.text)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-25 07:10:26

您需要在find方法中考虑到命名空间。

代码语言:javascript
复制
from xml.etree import cElementTree as ET
xmlstr = """<?xml version='1.0' encoding='UTF-8'?>

<tns:getCamerasResponse xmlns:tns="https://infoconnect.highwayinfo.govt.nz/schemas/camera2">

    <tns:camera>

        <tns:description>North along Sth Wstn Mwy from May Rd</tns:description>

        <tns:direction>Northbound</tns:direction>

        <tns:group>NA</tns:group>

        <tns:id>653</tns:id>

        <tns:imageUrl>http://www.trafficnz.info/camera/653.jpg</tns:imageUrl>

        <tns:lat>-36.90943</tns:lat>

        <tns:lon>174.73442</tns:lon>

        <tns:name>SH20 May Rd Overbridge</tns:name>

        <tns:offline>false</tns:offline>

        <tns:region>Auckland</tns:region>

        <tns:thumbUrl>http://www.trafficnz.info/camera/thumb/653.jpg</tns:thumbUrl>

        <tns:underMaintenance>false</tns:underMaintenance>

        <tns:viewUrl>http://www.trafficnz.info/camera/view/653</tns:viewUrl>

 </tns:camera>
 </tns:getCamerasResponse>"""



root = ET.fromstring(xmlstr)

ns = {'tns':"https://infoconnect.highwayinfo.govt.nz/schemas/camera2"}
results = root.findall('tns:camera', ns)
for camera in results:
    region = camera.find('tns:region', ns)
    if region is not None:
        print(region.text)

如果您需要知道如何获取名称空间,这可能会对您有所帮助,比如Python: ElementTree, get the namespace string of an Element

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58089012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档