import csv import requests from bs4 import BeautifulSoup servel_html = requests.get('http://www.servel.cl/declaraciones-de-patrimonio-e-intereses-alcaldes/') page_servel = BeautifulSoup(servel_html.text, 'lxml') with open("candidatos.csv", 'a+') as f: w = csv.writer(f) for tab_id in range(0, 52): comuna_a = page_servel.find('a', {'href': "#collapse936{}".format(tab_id)}) comuna = comuna_a.find('h5').get_text() candidatos_div = page_servel.find(attrs={'id': "collapse936{}".format(tab_id)}) candidatos = candidatos_div.findAll('a') for candidato in candidatos: w.writerow(( comuna.encode('utf-8'), candidato.get_text().encode('utf-8'), candidato.get('href'), candidato.find_next('em').get_text().encode('utf-8') ))