def stage_clasification_parser(stage_number): data={}; data_order=["pos","rider","country","team","time"] data_order_team=["pos","team","time"] all_class_order=["stage","general","mountain","team","regularity"] urlfetch.set_default_fetch_deadline(45) images_json=[] if(int(stage_number)>9): url=("http://www.marca.com/deporte/ciclismo/tour/clasificaciones/etapa%s.html" % stage_number) else: url=("http://www.marca.com/deporte/ciclismo/tour/clasificaciones/etapa0%s.html" % stage_number) page = urllib2.urlopen(url) soup = BeautifulSoup(page, "html.parser") tables = soup.findAll("table",{"class":"clasificacion_etapa"}) table_count=0 for table in tables: stage_clas=[] for row in table.findAll('tr'): single_row={} col = row.findAll('td') cont=0 for td in col: if table_count==3: single_row[data_order_team[cont]]=td.string else: single_row[data_order[cont]]=td.string cont+=1 if single_row: stage_clas.append(single_row) data[all_class_order[table_count]]=stage_clas table_count+=1 stage_classification = StageClasification(id=stage_number,data=json.dumps(data)) stage_classification.put()
def get(self): stage_key = self.request.get('stage') stage_details=StageClasification.get_by_id(stage_key) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(json.loads(stage_details.data)))