【发布时间】:2017-05-29 17:19:33
【问题描述】:
我必须从该网站获取所有 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)
【问题讨论】:
标签: python xml xml-parsing html-parsing