我必须从这个网站获取XML中的所有“活动”: http://www.bcn.cat/tercerlloc/agenda_cultural.xml
为了做到这一点,我创建了这个类,它将保留XML中的新活动:
class GeoLoc(object):
def __init__(self, adresa, lat, lon):
self.adresa = adresa
self.lat = lat
self.lon = lon
def valid(self):
return self.lat !="" and self.lon != ""
class Acte(GeoLoc):
nom = ""
def __init__(self, line):
super(Acte, self).__init__(line[0], line[1], line[2])
self.nom = line[3]
事情是我不知道如何获得这个值,例如:
<row num="9" pos="8">
(行num和pos的值)以及如何到达此地址
<address label="Adreça">
<![CDATA[Pl Glòries Catalanes 37
我想有这样的东西(我将它用于不同的xml):
sock = urllib.request.urlopen("http://wservice.viabicing.cat/getstations.php?v=1")
xmlSource = sock.read()
sock.close()
root = ET.fromstring(xmlSource)
estaciones = []
#obtenemos las estaciones de bicing
for element in root.findall('station'):
elements = []
if (element.find('streetNumber').text != None):
elements.append(element.find('street').text + " - " + element.find('streetNumber').text)
答案 0 :(得分:0)
您需要使用参数:strip_cdata=False
解析XML以保留CDATA,可以使用.attrib
属性来访问标记属性。请查看lxml docs/tutorial。
parser = etree.XMLParser(strip_cdata=False)
root = etree.XML(sock, parser)
for element in root.findall('.//row'):
addr = element.find('.//address')
if addr is not None:
print element.attrib
print addr.text
print ""