-
Notifications
You must be signed in to change notification settings - Fork 10
/
cei.py
210 lines (159 loc) · 6.28 KB
/
cei.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python
import csv
import requests
import time
import yaml
import urllib3
from datetime import datetime
from bs4 import BeautifulSoup
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
print("Starting...{}".format(datetime.now()))
with open("config.yaml", 'r') as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
cpf = config['cpf']
senha = config['senha']
login_url = "https://ceiapp.b3.com.br/cei_responsivo/login.aspx"
home_url = "https://ceiapp.b3.com.br/CEI_Responsivo/home.aspx"
negociacao_url = "https://ceiapp.b3.com.br/CEI_Responsivo/negociacao-de-ativos.aspx"
session_requests = requests.session()
#abre login para caturar tokens
result = session_requests.get(login_url,verify=False)
soup = BeautifulSoup(result.text, "html.parser")
view_state = soup.find(id='__VIEWSTATE')['value']
viewstate_generator = soup.find(id='__VIEWSTATEGENERATOR')['value']
event_validation = soup.find(id='__EVENTVALIDATION')['value']
payload = {
"__EVENTTARGET": "",
"__EVENTARGUMENT": "",
"ctl00$ContentPlaceHolder1$txtLogin": cpf,
"ctl00$ContentPlaceHolder1$txtSenha": senha,
"__VIEWSTATEGENERATOR": viewstate_generator,
"__EVENTVALIDATION": event_validation,
"__VIEWSTATE": view_state,
"ctl00$ContentPlaceHolder1$btnLogar": "Entrar"
}
#efetua login
result = session_requests.post(
login_url,
data = payload,
headers = dict(referer=login_url)
)
#abre negociacao de ativos
result = session_requests.get(
negociacao_url,
headers = dict(referer=home_url)
)
soup = BeautifulSoup(result.text, "html.parser")
data_inicio = soup.find(id='ctl00_ContentPlaceHolder1_txtDataDeBolsa')['value']
data_fim = soup.find(id='ctl00_ContentPlaceHolder1_txtDataAteBolsa')['value']
conta = "0"
#percorre todos os agentes (corretoras)
agentes = soup.find(id='ctl00_ContentPlaceHolder1_ddlAgentes').find_all('option')
for agente_aux in agentes:
view_state = soup.find(id='__VIEWSTATE')['value']
viewstate_generator = soup.find(id='__VIEWSTATEGENERATOR')['value']
event_validation = soup.find(id='__EVENTVALIDATION')['value']
agente = agente_aux['value']
print("Mudando agente => agente: " + agente)
payload = {
"ctl00$ContentPlaceHolder1$ToolkitScriptManager1": "ctl00$ContentPlaceHolder1$updFiltro|ctl00$ContentPlaceHolder1$ddlAgentes",
"ctl00_ContentPlaceHolder1_ToolkitScriptManager1_HiddenField": "",
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$ddlAgentes",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"ctl00$ContentPlaceHolder1$hdnPDF_EXCEL": "",
"__VIEWSTATEGENERATOR": viewstate_generator,
"__EVENTVALIDATION": event_validation,
"__VIEWSTATE": view_state,
"ctl00$ContentPlaceHolder1$txtDataDeBolsa": data_inicio,
"ctl00$ContentPlaceHolder1$txtDataAteBolsa": data_fim,
"ctl00$ContentPlaceHolder1$ddlContas": conta,
"ctl00$ContentPlaceHolder1$ddlAgentes": agente
}
#seleciona agente (corretora)
result = session_requests.post(
negociacao_url,
data = payload,
headers = dict(referer=negociacao_url)
)
soup = BeautifulSoup(result.text, "html.parser")
#percorre todas as contas
contas = soup.find(id='ctl00_ContentPlaceHolder1_ddlContas').find_all('option')
for conta_aux in contas:
view_state = soup.find(id='__VIEWSTATE')['value']
viewstate_generator = soup.find(id='__VIEWSTATEGENERATOR')['value']
event_validation = soup.find(id='__EVENTVALIDATION')['value']
conta = conta_aux['value']
print("Mudando conta => agente: " + agente + " | conta: " + conta)
payload = {
"ctl00$ContentPlaceHolder1$ToolkitScriptManager1": "ctl00$ContentPlaceHolder1$updFiltro|ctl00$ContentPlaceHolder1$ddlAgentes",
"ctl00_ContentPlaceHolder1_ToolkitScriptManager1_HiddenField": "",
"__EVENTTARGET": "ctl00$ContentPlaceHolder1$ddlAgentes",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"ctl00$ContentPlaceHolder1$hdnPDF_EXCEL": "",
"__VIEWSTATEGENERATOR": viewstate_generator,
"__EVENTVALIDATION": event_validation,
"__VIEWSTATE": view_state,
"ctl00$ContentPlaceHolder1$txtDataDeBolsa": data_inicio,
"ctl00$ContentPlaceHolder1$txtDataAteBolsa": data_fim,
"ctl00$ContentPlaceHolder1$ddlContas": conta,
"ctl00$ContentPlaceHolder1$ddlAgentes": agente,
"ctl00$ContentPlaceHolder1$btnConsultar": "Consultar"
}
#seleciona conta e retorna resultado da busca
result = session_requests.post(
negociacao_url,
data = payload,
headers = dict(referer=negociacao_url)
)
soup = BeautifulSoup(result.text, "html.parser")
##### GRAVAR AQUI O RESULTADO
data = []
table = soup.find(id="ctl00_ContentPlaceHolder1_rptAgenteBolsa_ctl00_rptContaBolsa_ctl00_pnAtivosNegociados")
if table != None:
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
colsd = [ele.text.replace('.','').replace(',','.').strip() for ele in cols]
colsd.append(agente)
colsd.append(conta)
data.append([ele for ele in colsd])
file = open("CEIHIST-" + agente + "_" + conta + ".csv", "w")
wtr = csv.writer(file, delimiter=';', lineterminator='\n')
for x in data : wtr.writerow(x)
file.close()
#####
#reinicializa busca
view_state = soup.find(id='__VIEWSTATE')['value']
viewstate_generator = soup.find(id='__VIEWSTATEGENERATOR')['value']
event_validation = soup.find(id='__EVENTVALIDATION')['value']
payload = {
"ctl00$ContentPlaceHolder1$ToolkitScriptManager1": "ctl00$ContentPlaceHolder1$updFiltro|ctl00$ContentPlaceHolder1$btnConsultar",
"ctl00$ContentPlaceHolder1$btnConsultar": "Nova Consulta",
"ctl00_ContentPlaceHolder1_ToolkitScriptManager1_HiddenField": "",
"__EVENTTARGET": "",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"ctl00$ContentPlaceHolder1$hdnPDF_EXCEL": "",
"__VIEWSTATEGENERATOR": viewstate_generator,
"__EVENTVALIDATION": event_validation,
"__VIEWSTATE": view_state,
"ctl00$ContentPlaceHolder1$txtDataDeBolsa": data_inicio,
"ctl00$ContentPlaceHolder1$txtDataAteBolsa": data_fim,
"ctl00$ContentPlaceHolder1$ddlContas": conta,
"ctl00$ContentPlaceHolder1$ddlAgentes": agente,
"ctl00$ContentPlaceHolder1$btnConsultar": "Nova Consulta"
}
result = session_requests.post(
negociacao_url,
data = payload,
headers = dict(referer=negociacao_url)
)
soup = BeautifulSoup(result.text, "html.parser")
print("Finish...{}".format(datetime.now()))
time.sleep(1)