-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmeDaily.py
60 lines (47 loc) · 1.79 KB
/
cmeDaily.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
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from openpyxl import load_workbook
import datetime
def executeRobotCME(url):
driver.get(url)
time.sleep(2)
element = driver.find_element_by_xpath("//table[@id='quotesFuturesProductTable1']")
htmlContent = element.get_attribute('outerHTML')
soup = BeautifulSoup(htmlContent, 'html.parser')
table = soup.find(name='table')
df = pd.read_html(str(table))[0].head(1)
df.reset_index(drop=True, inplace=True)
dictIndicadores = df.to_dict('records')
return dictIndicadores
def writeExcel(filename, dictInsData, sheet, date):
wb = load_workbook(filename, False)
ws = wb.get_sheet_by_name(sheet)
cellPos = ws.max_row
for data in dictInsData:
if ws['B' + str(cellPos)].value == data['Last']:
ws['C' + str(cellPos)].value = date
else:
cellPos = cellPos + 1
ws['A' + str(cellPos)].value = data['Month']
ws['B' + str(cellPos)].value = data['Last']
ws['C' + str(cellPos)].value = date
wb.save(filename)
wb.close()
if __name__ == '__main__':
#Carregando Variaveis
urlCME = 'https://www.cmegroup.com/trading/fx/emerging-market/brazilian-real.html'
excelFilename = 'RawDataCME.xlsx'
option = Options()
option.headless = True
driver = webdriver.Chrome(options=option)
try:
while True:
dateToday = datetime.datetime.now()
print('Processando CME: ' + urlCME)
dictIndicadores = executeRobotCME(url=urlCME)
writeExcel(filename=excelFilename, dictInsData=dictIndicadores, sheet='CME', date=dateToday)
except KeyboardInterrupt:
driver.quit()