Last active
December 21, 2021 21:49
-
-
Save berlotto/54d1e4e59257f9c498f9 to your computer and use it in GitHub Desktop.
Numeros da MegaSena
This file contains hidden or 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
# -*- encoding: utf-8 -*- | |
# | |
# Python Script - Numeros da Megasena | |
# Faz o download do arquivo da Caixa Economica Federal, com todos os resultados | |
# da Mega já obtidos. Faz uma contagem simples e mostras, em ordem decrescente, | |
# a quantidade de vezes que os nros já foram sorteados. | |
# | |
# Author: Sergio Berlotto <[email protected]> | |
# Author website: http://berlotto.github.io | |
# Licence: GPLv3 | |
# | |
# Need to install requests and beautifulsoup4 lib. | |
import requests | |
import zipfile | |
import os | |
import collections | |
from bs4 import BeautifulSoup | |
import sys | |
STORAGE_PATH = "/tmp/mega" | |
FILENAME = "resultados.zip" | |
FILE_PATH = os.path.join(STORAGE_PATH, FILENAME) | |
INSIDE_FILENAME = "d_megasc.htm" | |
URL = "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_mgsasc.zip" | |
if not os.path.isdir(STORAGE_PATH): | |
try: | |
os.makedirs(STORAGE_PATH) | |
except OSError, e: | |
print "ERRO: Nao foi possivel criar o diretorio '%s'.\nVerifique!" | |
sys.exit() | |
print "- Baixano o arquivo de resultados..." | |
f = requests.get(URL) | |
with open(FILE_PATH, "wb") as myzip: | |
myzip.write(f.content) | |
myzip.close() | |
print "- Pronto! Extraindo..." | |
if zipfile.is_zipfile(FILE_PATH): | |
zip = zipfile.ZipFile(FILE_PATH) | |
zip.extract(INSIDE_FILENAME) | |
print "- Abrindo arquivo para buscar os resultados..." | |
html = open(INSIDE_FILENAME).read() | |
soup = BeautifulSoup(html) | |
tds = lambda node: [ x.text for x in node if x not in ('\n', ' ')] | |
nros = [ tds(x)[2:8] for x in soup.body.table.contents[2:] if x not in ('\n', ' ')] | |
print "- Verificando os numeros mais utilizados..." | |
usados = dict([ (x,0) for x in range(1,61)]) | |
for sorteio in nros: | |
for nro in sorteio: | |
usados[ int(nro) ] += 1 | |
usados_ordenados = collections.OrderedDict([ (y,usados[y]) for y in sorted(usados, key=usados.get, reverse=True) ]) | |
print "Os nros que mais foram sorteados na MEGASENA:" | |
for k in usados_ordenados.keys(): | |
print "%02d" % k, ":", usados_ordenados[k], "vezes" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment