-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandidatosPrefeitosNoTse.py
154 lines (94 loc) · 3.64 KB
/
candidatosPrefeitosNoTse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
# coding: utf-8
# In[59]:
#importa as bibliotecas
from selenium import webdriver
from time import sleep
import pickle #https://www.semicolonworld.com/question/43757/how-to-save-and-load-cookies-using-python-selenium-webdriver
import random
from bs4 import BeautifulSoup
import pandas as pd
import re
# In[27]:
#acessa o driver do Chrome
driver = webdriver.Chrome("<FOLDER DO CHROMEDRIVE EM SEU EQUIPAMENTO>chromedriver.exe")
# In[28]:
#acessa o link inicial dos prefeitos de SCS
driver.get('http://divulgacandcontas.tse.jus.br/divulga/#/municipios/2020/2030402020/70777/candidatos') #MUDE PARA O CAMINHO DE SUA CIDADE
driver.maximize_window()
# In[29]:
#pega a parte central
elemento = driver.find_element_by_xpath("/html/body/div[2]/div[1]/div/div/section[3]/div/div/table[2]")
# In[30]:
#pega o conteúdo do bloco
html_content = elemento.get_attribute('outerHTML')
#print(html_content)
# In[31]:
#aciona o soup para facilitar o tratamento
soup = BeautifulSoup(html_content, 'html.parser')
#soup
# In[38]:
candidatos = pd.DataFrame(columns=["nome", "coligacao", "situacao", "nome_urna", "cnpj", "total_bens", "dinheiro"])
# In[39]:
cadaUm = soup.find_all("td", class_=lambda x: x != "text-center")
conta = 0
for i in cadaUm:
conta += 1
pedacos = i.find_all("div")
pedaco = 0
for d in pedacos:
pedaco += 1
if pedaco == 1:
nome = d.get_text()
elif pedaco == 2:
coligacao = d.get_text()
elif pedaco == 3:
situacao = d.get_text()
nome_urna = i.strong.get_text()
print(nome_urna)
candidatos.loc[conta] = ({"nome": nome, "coligacao": coligacao, "situacao": situacao, 'nome_urna': nome_urna})
print(conta)
# In[40]:
candidatos.head(10)
# In[76]:
#busca dados da página individual
for i in range(1, conta + 1):
ponto = '/html/body/div[2]/div[1]/div/div/section[3]/div/div/table[1]/tbody/tr[' + str(i) + ']/td[1]/a'
driver.find_element_by_xpath(ponto).click()
sleep(3)
#pega o body
paginaIndividual = driver.find_element_by_tag_name("body")
htmlIndividual = paginaIndividual.get_attribute('outerHTML')
soupIndividual = BeautifulSoup(htmlIndividual, 'html.parser')
cnpj = soupIndividual.find("tr", {"data-ng-if" : "resultData.cnpjcampanha"}).get_text().replace("CNPJ - ", "")
print(cnpj)
candidatos.loc[i, 'cnpj'] = cnpj
#vai para um nível mais abaixo para pegar o patrimônio
ponto = '/html/body/div[2]/div[1]/div/div[1]/section[3]/div/div[1]/div[2]/button[1]'
driver.find_element_by_xpath(ponto).click()
sleep(3)
paginaPatrimonio = driver.find_element_by_tag_name("body")
#print(paginaPatrimonio)
htmlPatromonio = paginaPatrimonio.get_attribute('outerHTML')
#print(htmlPatromonio)
soupPatrimonio = BeautifulSoup(htmlPatromonio, 'html.parser')
#print(soupPatrimonio)
patrimonio = soupPatrimonio.find("div", {"class" : "dvg-painel-azul"}).find("span").get_text()
print(patrimonio)
candidatos.loc[i, 'total_bens'] = patrimonio.replace("R$", "").replace(".", "")
try:
temDinheiro = soupPatrimonio.find("span", text = re.compile("^Dinheiro em espécie - moeda nacional.*"), attrs = {"class" : "ng-binding"})
dinheiro = temDinheiro.parent.findNext('td').find("span").get_text()
except:
dinheiro = "0"
print(dinheiro)
candidatos.loc[i, 'dinheiro'] = dinheiro.replace("R$", "").replace(".", "")
driver.execute_script("window.history.go(-1)")
sleep(3)
driver.execute_script("window.history.go(-1)")
sleep(3)
# In[74]:
candidatos.head(10)
# In[75]:
candidatos.to_excel("prefeitos.xls")
# In[ ]: