import xml.etree.cElementTree as ET class Station_on_line: def __init__(self,distance,name,link): self.distance = distance self.name = name self.link = link @classmethod def fromXML(cls,station_on_line_element): distance=float(station_on_line_element.attrib["Distance"]) name=station_on_line_element.attrib["Name"] link=station_on_line_element.attrib["Link"] return(cls(distance, name, link)) def toXML(self): station_on_line_element = ET.Element("station_on_line") station_on_line_element.attrib["Distance"]=str(self.distance) station_on_line_element.attrib["Name"]=self.name station_on_line_element.attrib["Link"]=self.link return(station_on_line_element) #This will work in most cases. For some lines #it does not work. In this case the data #has to be filled in manually def soup_to_station_on_line(soup): columns = soup.findAll("td") if(len(columns)<3): return(None) #The Dutch use a comma , instead of a point . for floating #point numbers distance_string = columns[1].text.replace(",",".") distance = float(distance_string) name_soup = columns[2].find('a') link = name_soup.attrs['href'] name = name_soup.text station_on_line = Station_on_line(distance,name,link) return(station_on_line)