zone = zone.replace("Zone ","") #strip Zone prefix addl = stop.find("h4", text="Additional Information").find_next_sibling().get_text() addl = addl.replace("\n","") #strip newlines mapurl = stop.find("a", text="View Stop On Map").get("href") #?lat=42.393639&lon=-76.362834&stopid=9368 mapquery = parse_qsl(urlparse(mapurl).query) #[('lat', '42.458150'), ('lon', '-76.475667'), ('stopid', '3660')] lat = mapquery[0][1] lon = mapquery[1][1] stopid = mapquery[2][1] #create the stop object stops.append(Stop(name, stopid, area, zone, addl, lat, lon)) print("Data collection finished.\nCollected {} stops.\nBegin writing to file.".format(len(stops))) f = open('TCATstops.csv', 'w') f.write("id,name,zone,lat,lon,area,addl\n") #iterate through stop objects for stop in stops: f.write(stop.toCSV()) f.close() print("Completed")