Skip to content

Instantly share code, notes, and snippets.

@pmarkun
Created March 8, 2019 21:14
Show Gist options
  • Select an option

  • Save pmarkun/12296625ccd1bcdc42032e0a0abdf6b0 to your computer and use it in GitHub Desktop.

Select an option

Save pmarkun/12296625ccd1bcdc42032e0a0abdf6b0 to your computer and use it in GitHub Desktop.
from lxml.html import parse
from urllib import request
import csv
def get_info_autor(autor):
soup = parse(request.urlopen(autor['url'])).getroot()
p = soup.cssselect("#infoGeral")[0]
autor['partido'] = p.xpath('.//label[text()=" Partido "]')[0].getnext().text.strip()
return autor
def get_detalhes(pl):
soup = parse(request.urlopen(pl['url'])).getroot()
d = soup.cssselect(".tabelaDados")[0]
pl['documento'] = d.cssselect(".campo_conteudo a")[0].get('href')
pl['numero'] = d.xpath('.//td[text()="Número Legislativo"]')[0].getnext().text.split('/')[0].strip()
pl['ano'] = d.xpath('.//td[text()="Número Legislativo"]')[0].getnext().text.split('/')[1].strip()
pl['regime'] = d.xpath('.//td[text()="Regime"]')[0].getnext().text.strip()
pl['apoiadores'] = d.xpath('.//td[text()="Apoiador(es)"]')[0].getnext().text_content().strip()
if d.xpath('.//td[text()="Transformado em Norma"]'):
pl['norma'] = 1
else:
pl['norma'] = 0
return pl
def getPLs(autores):
base_url = "https://www.al.sp.gov.br/alesp/pesquisa-proposicoes/"
args = "?direction=inicio&lastPage=5&act=detalhe&idDocumento=&rowsPerPage=1000&currentPageDetalhe=1&tpDocumento=&method=search&text=Declara%20de%20UTILIDADE%2520P%25C3%259ABLICA&natureId=1&currentPage="
soup = parse(request.urlopen(base_url+args+str(0))).getroot()
lista_pl = []
count = 0
for x in soup.cssselect("#lista_resultado .tabela tr a")[1:]:
pl = {}
pl['url'] = 'https://www.al.sp.gov.br' + x.get('href')
pl['titulo'] = x.cssselect("strong")[0].text
pl['ementa'] = x.cssselect("br")[0].tail.strip()
pl['autor'] = x.getparent().getnext().text.split('\n')[0]
if pl['autor'] in autores:
pl['partido'] = autores[pl['autor']]['partido']
pl['area'] = autores[pl['autor']]['area']
pl['base'] = autores[pl['autor']]['base']
else:
pass
pl = get_detalhes(pl)
count += 1
if count > 100:
print ("Got 10!")
count = 0
lista_pl.append(pl)
return lista_pl
def getAutores():
url = "https://www.al.sp.gov.br/alesp/deputados-estaduais/?filtroNome=&filtroAreaAtuacao=&filtroBaseEleitoral=&filtroPartido=&filtroLegislatura=18&filtroEmExercicioPesquisa=N&filtroLegislaturaAtual=S"
soup = parse(request.urlopen(url)).getroot()
linhas = soup.cssselect(".tabela tbody tr")
autores = {}
for p in linhas:
nome = p.xpath('.//td/a')[0].text.strip()
autores[nome] = {}
autores[nome]['nome'] = p.xpath('.//td/a')[0].text.strip()
autores[nome]['area'] = p.xpath('.//td')[1].text.strip()
autores[nome]['base'] = p.xpath('.//td')[2].text.strip()
autores[nome]['partido'] = p.xpath('.//td')[3].text.strip()
print("Getting "+nome)
return autores
def escrevecsv(lista_pl):
with open('projetos.csv', 'w') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, lista_pl[0].keys())
w.writeheader()
for row in lista_pl:
w.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment