Skip to content

Instantly share code, notes, and snippets.

@0xbepresent
Created October 18, 2016 19:40
Show Gist options
  • Save 0xbepresent/a261f44f6d11a0da1a1152f06f1aba53 to your computer and use it in GitHub Desktop.
Save 0xbepresent/a261f44f6d11a0da1a1152f06f1aba53 to your computer and use it in GitHub Desktop.
Obtener candidatos de Santiago.
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')
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment