-
Notifications
You must be signed in to change notification settings - Fork 9
/
fii-lista.py
120 lines (85 loc) · 3.18 KB
/
fii-lista.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# fii-lista.py
#
# Copyright (C) 2015 Caian Benedicto <[email protected]>
#
# This file is part of bovespa-tools
#
# bovespa-tools is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# Asparagus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import tprinting
import time
import argparse
###############################################################################
# Alguma definições
###############################################################################
baseurl = "http://www.bmfbovespa.com.br/fundos-listados/fundoslistados.aspx"
queryurl = baseurl + "?Idioma=pt-br&tipoFundo=imobiliario"
matext = '.mat'
clicktimeout = 10
timeoutsleep = 1
headerxpath = "//*[@class='tabela tabFundos']//table//thead//tr//th"
tablexpath = "//*[@class='tabela tabFundos']//table//tr//td"
asserttitle = 'Fundos Imobiliários'
assertheader = (
'Razão Social',
'Fundo',
'Segmento',
'Código'
)
###############################################################################
# Rotina principal
###############################################################################
def main(outfile):
# Cria um driver do Firefox (mude pro seu navegador), abre a
# página de query e verifica se está na página certa
driver = webdriver.Firefox()
driver.get(queryurl)
assert driver.title == asserttitle
# Lê o cabeçalho da tabela e o conteúdo
for i in range(clicktimeout):
header = [elem.text for
elem in
driver.find_elements_by_xpath((headerxpath))]
if len(header) != 0:
break
time.sleep(timeoutsleep)
table = [elem.text for
elem in
driver.find_elements_by_xpath((tablexpath))]
# Fecha a janela
driver.close()
# Valida os cabeçalhos, dados da tabela e links
assert len(header) >= len(assertheader)
for i in range(len(assertheader)):
assert header[i] == assertheader[i]
assert len(table) % len(header) == 0
# Divide a tabela
table2 = [table[k:k+len(header)] for
k in
range(0,len(table),len(header))]
# Imprime a tabela
if outfile.endswith(matext):
tprinting.writeMat(outfile, header, table2)
else:
tprinting.writeTable(outfile, header, table2)
###############################################################################
# Ponto de entrada do script
###############################################################################
if __name__ == '__main__':
# Parsing dos argumentos de entrada
parser = argparse.ArgumentParser()
parser.add_argument('--out', type=str, default='', help='arquivo de saida')
args = parser.parse_args()
main(args.out)