Created
February 28, 2016 14:51
-
-
Save phoemur/279c8bd998a5b11282d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import urllib.request | |
import urllib.parse | |
import http.cookiejar | |
from lxml.html import fragment_fromstring | |
from collections import OrderedDict | |
def get_data(*args, **kwargs): | |
url = 'http://www.fundamentus.com.br/resultado.php' | |
cj = http.cookiejar.CookieJar() | |
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) | |
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201'), | |
('Accept', 'text/html, text/plain, text/css, text/sgml, */*;q=0.01')] | |
# Aqui estão os parâmetros de busca das ações | |
# Estão em branco para que retorne todas as disponíveis | |
data = {'pl_min':'', | |
'pl_max':'', | |
'pvp_min':'', | |
'pvp_max' :'', | |
'psr_min':'', | |
'psr_max':'', | |
'divy_min':'', | |
'divy_max':'', | |
'pativos_min':'', | |
'pativos_max':'', | |
'pcapgiro_min':'', | |
'pcapgiro_max':'', | |
'pebit_min':'', | |
'pebit_max':'', | |
'fgrah_min':'', | |
'fgrah_max':'', | |
'firma_ebit_min':'', | |
'firma_ebit_max':'', | |
'margemebit_min':'', | |
'margemebit_max':'', | |
'margemliq_min':'', | |
'margemliq_max':'', | |
'liqcorr_min':'', | |
'liqcorr_max':'', | |
'roic_min':'', | |
'roic_max':'', | |
'roe_min':'', | |
'roe_max':'', | |
'liq_min':'', | |
'liq_max':'', | |
'patrim_min':'', | |
'patrim_max':'', | |
'divbruta_min':'', | |
'divbruta_max':'', | |
'tx_cresc_rec_min':'', | |
'tx_cresc_rec_max':'', | |
'setor':'', | |
'negociada':'ON', | |
'ordem':'1', | |
'x':'28', | |
'y':'16'} | |
with opener.open(url, urllib.parse.urlencode(data).encode('UTF-8')) as link: | |
content = link.read().decode('ISO-8859-1') | |
pattern = re.compile('<table id="resultado".*</table>', re.DOTALL) | |
reg = re.findall(pattern, content)[0] | |
page = fragment_fromstring(reg) | |
lista = OrderedDict() | |
for rows in page.xpath('tbody')[0].findall("tr"): | |
lista.update({rows.getchildren()[0][0].getchildren()[0].text: {'cotacao': rows.getchildren()[1].text, | |
'P/L': rows.getchildren()[2].text, | |
'P/VP': rows.getchildren()[3].text, | |
'PSR': rows.getchildren()[4].text, | |
'DY': rows.getchildren()[5].text, | |
'P/Ativo': rows.getchildren()[6].text, | |
'P/Cap.Giro': rows.getchildren()[7].text, | |
'P/EBIT': rows.getchildren()[8].text, | |
'P/Ativ.Circ.Liq.': rows.getchildren()[9].text, | |
'EV/EBIT': rows.getchildren()[10].text, | |
'EBITDA': rows.getchildren()[11].text, | |
'Mrg.Liq.': rows.getchildren()[12].text, | |
'Liq.Corr.': rows.getchildren()[13].text, | |
'ROIC': rows.getchildren()[14].text, | |
'ROE': rows.getchildren()[15].text, | |
'Liq.2m.': rows.getchildren()[16].text, | |
'Pat.Liq': rows.getchildren()[17].text, | |
'Div.Brut/Pat.': rows.getchildren()[18].text, | |
'Cresc.5a': rows.getchildren()[19].text}}) | |
return lista | |
if __name__ == '__main__': | |
fundamentus = get_data() | |
lista_pl = list() | |
# Remover PL negativo e ROE negativo | |
for k, v in fundamentus.items(): | |
if float(v['P/L'].replace('.', '').replace(',','.')) <= 0: | |
continue | |
if float(v['ROE'].replace('.', '').replace(',','.').rstrip('%')) <= 0: | |
continue | |
else: | |
lista_pl.append([k, v['P/L'], v['ROE']]) | |
# Criar lista ROE | |
lista_roe = sorted(lista_pl, key=lambda x:float(x[2].replace('.', '').replace(',','.').rstrip('%'))) | |
lista_roe.reverse() | |
# Lista resultado: Ativo, PL, ROE, Ranking PL, Ranking ROE, Ranking GreenBlat | |
lista_final = list() | |
for i in lista_pl: | |
n_pl = lista_pl.index(i) + 1 | |
n_roe = lista_roe.index(i) + 1 | |
lista_final.append([i[0], i[1], i[2], n_pl, n_roe, n_pl + n_roe]) | |
lista_final.sort(key=lambda x: x[5]) | |
print('{0:<8} {1:<8} {2:<10} {3:<11} {4:<13} {5:<18}'.format('Ativo', | |
'PL', | |
'ROE', | |
'Ranking PL', | |
'Ranking ROE', | |
'Ranking GreenBlat')) | |
print('-'*73) | |
for i in lista_final: | |
print('{0:<8} {1:<8} {2:<10} {3:<11} {4:<13} {5:<18}'.format(i[0], | |
i[1], | |
i[2], | |
i[3], | |
i[4], | |
i[5])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ordena as empresas do IBOVESPA segundo a fórmula de GreenBlat (The little book that beats the market) utilizando dados do site fundamentus (www.fundamentus.com.br)