Skip to content

Instantly share code, notes, and snippets.

@JuniorPolegato
Created August 28, 2014 13:57
Show Gist options
  • Save JuniorPolegato/52dd3d669cb00050c8eb to your computer and use it in GitHub Desktop.
Save JuniorPolegato/52dd3d669cb00050c8eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pole
import zipfile
# Identificar tipo e converter
def converter(no_xml):
string = str(no_xml)
if '/' in string:
return string
if string.isdigit():
return int(string)
return float(string.replace('.', '').replace(',', '.'))
# Conectar ao servidor
conexao = pole.http.Conexao('www1.caixa.gov.br')
# Obter dados
dados = conexao.obter_dados('/loterias/_arquivos/loterias/D_lotfac.zip')
# Separar o conteúdo do cabeçalho e mostrar o cabeçalho
conteudo = dados['conteúdo']
dados['conteúdo'] = '<arquivo>'
for x in dados.items():
print '%s: %s' % x
# Fechar a conexão com o servidor
conexao.fechar()
# Salvar o arquivo obtido
print 'Arquivo tem', len(conteudo), 'bytes'
f = open('D_lotfac.zip', 'wb')
f.write(conteudo)
f.close()
print "Salvo!"
# Extrair o arquivo HTM do arquivo zip
print 'Extraindo...'
zfile = zipfile.ZipFile('D_lotfac.zip')
zfile.extract('D_LOTFAC.HTM')
# Importar o HTML e criar vetor com linhas da tabela
print 'Importando...'
html = pole.xml.importar(open('D_LOTFAC.HTM').read(), html = True)
linhas = pole.xml.procurar(html, 'tr')
# Para cada linha, montar dicionário de concursos
print 'Analisando...'
concursos = {}
# Primeira linha é o cabeçalho dentro da tag font
cabecalho = [str(c) for c in pole.xml.procurar(linhas[0], 'font')][1:]
# Demais linhas são dados dentro da tag td
for linha in linhas[1:]:
campos = [converter(c) for c in pole.xml.procurar(linha, 'td')]
# Primeiro campo é o concurso, demais dados casados com cabeçalho
concursos[campos[0]] = dict(zip(cabecalho, campos[1:]))
# Exemplo de uso
print '- - - - - - - Exemplo - - - - - -'
print 'Bola 8 do concurso 1.000:', concursos[1000]['Bola8']
print 'Ganhadores com 15 números no concurso 850:',
print concursos[850]['Ganhadores_15_Números']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment