Skip to content

Instantly share code, notes, and snippets.

@luanfonceca
Created August 1, 2020 00:15
Show Gist options
  • Save luanfonceca/737961d43926535910f0b69ee8e01ddf to your computer and use it in GitHub Desktop.
Save luanfonceca/737961d43926535910f0b69ee8e01ddf to your computer and use it in GitHub Desktop.
Vereadoras e Vereadores 2020 - Natal RN

Como instalar:

$ pip install -r requirements.txt

Como rodar:

$ python scrapper.py

O resultado disso, vai ser um csv com várias informações populadas e o resto é necessário ir catar, pq o site não ajuda muito. Exemplo de resposta:

$ python scrapper.py
----------------------------------------
Baixando: https://www.cmnat.rn.gov.br/vereadores/13
Nome: Ana Paula Araujo
NOME: Ana Paula de Araújo Correia
TELEFONE: 3232 8828 CEL: 99455-3170
E-MAIL: anapaulaaraujocorreia@gmail.com
----------------------------------------



----------------------------------------
Baixando: https://www.cmnat.rn.gov.br/vereadores/18
Nome: Aroldo Alves da Silva
TELEFONE: 3232-3860
----------------------------------------



----------------------------------------
Baixando: https://www.cmnat.rn.gov.br/vereadores/39
Nome: Ary Gomes
NOME: Ary Gomes do Nascimento
TELEFONE: 987076512
E-MAIL: arygomes12@hotmail.com
REDES SOCIAIS:
Instagram @arygomes12612
----------------------------------------

Depois de executado, eu fui página por página pegando o máximo de informação possível. As vezes a rede social não existe mais, as vezes o telefone não vem... Mas tem tudo atualizado aqui: https://docs.google.com/spreadsheets/d/1K8c4HL7KkZLUffW_wynuTxmS0_y8-0e5g_LMq72mK6M/edit#gid=0

requests==2.23.0
rows==0.4.1
import requests
from lxml import html
from collections import OrderedDict
from rows import fields, Table, export_to_csv
URL = 'https://www.cmnat.rn.gov.br/vereadores'
campos = OrderedDict([
('nome_politico', fields.TextField),
('nome', fields.TextField),
('partido', fields.TextField),
('telefone', fields.TextField),
('celular', fields.TextField),
('email', fields.TextField),
('foto', fields.TextField),
])
csv_de_vereadores = Table(fields=campos)
resposta_basica = requests.get(URL, stream=True)
resposta_basica.raw.decode_content = True
html_parseado = html.parse(resposta_basica.raw)
for url_de_vereador in html_parseado.xpath('/html/body/section[2]/div/div/div/div/a/@href'):
print('----------------------------------------')
print(f'Baixando: {url_de_vereador}')
resposta = requests.get(url_de_vereador, stream=True)
resposta.raw.decode_content = True
resposta = html.parse(resposta.raw)
nome_politico = resposta.xpath(
'/html/body/section[2]/div/div/div[2]/div[1]/div[1]/h2/text()'
)[0].strip()
partido = resposta.xpath(
'/html/body/section[2]/div/div/div[2]/div[1]/div[1]/h2/span/text()'
)[0]
foto = resposta.xpath(
'/html/body/section[2]/div/div/div[1]/div/@style'
)[0].replace('background: url(', '').replace(');', '')
csv_de_vereadores.append({
'nome_politico': nome_politico,
'partido': partido,
'foto': foto,
})
print(f'Nome: {nome_politico}')
print(
resposta.xpath(
'/html/body/section[2]/div/div/div[2]/div[2]/p'
)[0].text_content()
)
print('----------------------------------------')
print('\n\n')
export_to_csv(csv_de_vereadores, 'Vereadores - RN.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment