Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python名称空间解析XML_Python_Xml_Parsing_Lxml_Elementtree - Fatal编程技术网

用Python名称空间解析XML

用Python名称空间解析XML,python,xml,parsing,lxml,elementtree,Python,Xml,Parsing,Lxml,Elementtree,我想使用开放数据,xml存储在这里: 在这个论坛的帮助下,我编写了代码 from lxml import etree from urllib.request import urlopen root = etree.parse(urlopen(url)).getroot() ns = { 'd': 'http://datex2.eu/schema/2/2_0' } parking_area = root.xpath('//d:parkingAreaStatus', namespaces=

我想使用开放数据,xml存储在这里:

在这个论坛的帮助下,我编写了代码

from lxml import etree
from urllib.request import urlopen


root = etree.parse(urlopen(url)).getroot()


ns = { 'd': 'http://datex2.eu/schema/2/2_0' }

parking_area = root.xpath('//d:parkingAreaStatus', namespaces=ns)
parking_facility = root.xpath('//d:parkingFacilityStatus', namespaces=ns)

for pa in parking_area:
    area_ref = pa.find('d:parkingAreaReference', ns)
    ParkingspaceList.append(str((area_ref.get('id'))))


for pf in parking_facility:
    facility_ref = pf.find('d:parkingFacilityReference', ns)
    ParkingspaceList.append(str((facility_ref.get('id'))))
这似乎对pa有效,但对于pf,我收到了一条失败消息:

ParkingspaceList.appendstrfacility\u ref.获取“id” AttributeError:“非类型”对象没有属性“get”

有什么建议吗

致意

TR

在名称相同的parkingFacilityStatus元素中有parkingFacilityStatus元素,这相当令人困惑:

<parkingFacilityStatus>
    …
    <parkingFacilityReference targetClass="ParkingFacility" id="24278[Karstadt]" version="1.0"/>
    <parkingFacilityStatus>closed</parkingFacilityStatus>
    …
</parkingFacilityStatus>

而且很有效。

@theamokdog很高兴它起到了作用!
    <parkingFacilityStatus>closed</parkingFacilityStatus>
for pf in parking_facility:
    facility_ref = pf.find('d:parkingFacilityReference', ns)
    if facility_ref is not None:
        ParkingspaceList.append(str((facility_ref.get('id'))))