-
Notifications
You must be signed in to change notification settings - Fork 1
/
dolarPtax.py
152 lines (118 loc) · 5.31 KB
/
dolarPtax.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
import time
import pyperclip
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from openpyxl import load_workbook
from decimal import Decimal
import datetime
def executeRobotPtax(url, baseDate):
driver.get(url)
time.sleep(4)
#print(driver.page_source)
driver.find_element_by_xpath("//input[@id='RadOpcao' and @value='3']").click()
#pyperclip.copy(baseDate)
driver.find_elements_by_id('DATAINI')[0].clear()
#driver.find_elements_by_id('DATAINI')[0].send_keys(Keys.CONTROL,'v')
driver.find_elements_by_id('DATAINI')[0].send_keys(baseDate.replace('/',''))
driver.find_elements_by_xpath("//input[@title='Pesquisar']")[0].click()
msgErro = driver.find_elements_by_xpath("//*[@class='msgErro']")
if len(msgErro) != 0:
return -1
time.sleep(4)
element = driver.find_element_by_xpath("//table[@class='tabela']")
htmlContent = element.get_attribute('outerHTML')
soup = BeautifulSoup(htmlContent, 'html.parser')
table = soup.find(name='table')
df = pd.read_html(str(table))[0]
df.columns = df.columns.get_level_values(2)
df.columns = ['Hora', 'Tipo', 'Compra', 'Venda', 'Trash', 'Trash', 'Trash']
df.drop(['Trash'], axis=1, inplace=True)
df.drop(df.tail(1).index,inplace=True)
return df
def writeExcel(filename, dfData, sheet, date):
wb = load_workbook(filename, False)
ws = wb.get_sheet_by_name(sheet)
cellPos = ws.max_row
if cellPos > 1:
lastCell = 'E' + str(cellPos)
cellsSort = 'E2:' + lastCell
ws.auto_filter.add_sort_condition(cellsSort, descending=True)
startRow = ws.max_row
if startRow > 20:
lastRow = ws.max_row - 15
else:
lastRow = 1
for i in range(startRow, lastRow, -1):
cellDate = 'E' + str(i)
cellContent = ws[cellDate].value
if(type(cellContent) is str):
testDate1 = datetime.datetime.strptime(cellContent, '%d/%m/%Y')
testDate2 = datetime.datetime.strptime(date, '%d/%m/%Y')
if testDate1.date() == testDate2.date():
ws.delete_rows(i, 1)
cellPos = ws.max_row
for i in range(len(dfData)):
cellPos = cellPos + 1
ws['A' + str(cellPos)].value = dfData['Hora'][i]
ws['B' + str(cellPos)].value = dfData['Tipo'][i]
ws['C' + str(cellPos)].value = Decimal(dfData['Compra'][i])/10000
ws['D' + str(cellPos)].value = Decimal(dfData['Venda'][i])/10000
ws['E' + str(cellPos)].value = date
wb.save(filename)
wb.close()
def readExcel(filename, sheet):
wb = load_workbook(filename, False)
ws = wb.get_sheet_by_name(sheet)
cellName = 'A2'
baseDate = ws[cellName].value
wb.close()
return baseDate
def updateExcel(filename, sheet, date):
wb = load_workbook(filename, False)
ws = wb.get_sheet_by_name(sheet)
cellName = 'A2'
ws[cellName].value = date
wb.save(filename)
wb.close()
if __name__ == '__main__':
#Carregando Variaveis
urlPtax = 'https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?method=exibeFormularioConsultaBoletim'
excelFilename = 'RawDataPtax.xlsx'
while True:
try:
dateToday = datetime.datetime.now()
option = Options()
option.headless = True
option.add_argument("--log-level=3")
driver = webdriver.Chrome(options=option)
refDate = readExcel(filename=excelFilename, sheet='DataBase')
if(type(refDate) is str):
refDate = datetime.datetime.strptime(refDate, '%d/%m/%Y')
refDateFmt = str(refDate.day).zfill(2) + '/' + str(refDate.month).zfill(2) + '/' + str(refDate.year)
print('Executando Data Base: ' + refDateFmt)
print('Obtendo Ptax em: ' + urlPtax)
dfPtax = executeRobotPtax(url=urlPtax, baseDate=refDateFmt)
if isinstance(dfPtax, pd.DataFrame):
writeExcel(filename=excelFilename, dfData=dfPtax, sheet='Ptax', date=refDateFmt)
if any('Fech' in s for s in dfPtax['Tipo']):
print('Encerrando processamento dia: ' + refDateFmt)
refDate = refDate + datetime.timedelta(days=1)
refDateFmt = str(refDate.day).zfill(2) + '/' + str(refDate.month).zfill(2) + '/' + str(refDate.year)
print('Atualiando data base para: ' + refDateFmt)
updateExcel(filename=excelFilename, sheet='DataBase', date=refDateFmt)
else:
print('Data Base não possui dados de Ptax!')
if dateToday.date() > refDate.date():
refDate = refDate + datetime.timedelta(days=1)
refDateFmt = str(refDate.day).zfill(2) + '/' + str(refDate.month).zfill(2) + '/' + str(refDate.year)
print('Atualiando data base para: ' + refDateFmt)
updateExcel(filename=excelFilename, sheet='DataBase', date=refDateFmt)
driver.quit()
print('Sleeping...')
time.sleep(60)
except:
print("Ocorreu um problema, reiniciando!")
driver.quit()