Reputation: 39
I want to work with open data, the xml is stored here:
with help from this forum i wrote the code
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'))))
It seems like this works for "pa" but for "pf" i got a failure message:
ParkingspaceList.append(str((facility_ref.get('id')))) AttributeError: 'NoneType' object has no attribute 'get'
Any Tips?
best regards
TR
Upvotes: 0
Views: 139
Reputation: 1659
There are parkingFacilityStatus
elements inside the identically named parkingFacilityStatus
elements, which is rather confusing:
<parkingFacilityStatus>
…
<parkingFacilityReference targetClass="ParkingFacility" id="24278[Karstadt]" version="1.0"/>
<parkingFacilityStatus>closed</parkingFacilityStatus>
…
</parkingFacilityStatus>
It's the
<parkingFacilityStatus>closed</parkingFacilityStatus>
that gives the error, as that element does not have a parkingFacilityReference
subelement.
Use
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'))))
and it works.
Upvotes: 1