Created
April 26, 2015 00:24
-
-
Save arthuralvim/0779dda52e6d56d0d3eb to your computer and use it in GitHub Desktop.
Para baixar os arquivos de texto sobre as chuvas das estações meteorológicas. http://hidroweb.ana.gov.br/
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
# -*- coding: utf-8 -*- | |
""" | |
Hidroweb | |
http://hidroweb.ana.gov.br/ | |
Algumas bibliotecas terceiras deverão ser instaladas. | |
pip install requests | |
pip install beautifulsoup4 | |
Para rodar é só passar a lista com os códigos dos postos e executar "python hidroweb.py" no terminal. | |
Ele baixará todos os arquivos de texto gerado. | |
A saída gerada para o caso abaixo. | |
** 2851050 ** | |
** 2851050 ** (baixado) | |
** 2851050 ** (concluído) | |
** 2751025 ** | |
** 2751025 ** (baixado) | |
** 2751025 ** (concluído) | |
** 2849035 ** | |
** 2849035 ** (baixado) | |
** 2849035 ** (concluído) | |
** 2750004 ** | |
** 2750004 ** (baixado) | |
** 2750004 ** (concluído) | |
** 2650032 ** | |
** 2650032 ** (baixado) | |
** 2650032 ** (concluído) | |
** 2850015 ** | |
** 2850015 ** (baixado) | |
** 2850015 ** (concluído) | |
""" | |
import requests | |
import re | |
import shutil | |
from bs4 import BeautifulSoup | |
class Hidroweb(object): | |
url_estacao = 'http://hidroweb.ana.gov.br/Estacao.asp?Codigo={0}&CriaArq=true&TipoArq={1}' | |
url_arquivo = 'http://hidroweb.ana.gov.br/{0}' | |
def __init__(self, estacoes): | |
self.estacoes = estacoes | |
def montar_url_estacao(self, estacao, tipo=1): | |
return self.url_estacao.format(estacao, tipo) | |
def montar_url_arquivo(self, caminho): | |
return self.url_arquivo.format(caminho) | |
def montar_nome_arquivo(self, estacao): | |
return u'{0}.zip'.format(estacao) | |
def salvar_arquivo_texto(self, estacao, link): | |
r = requests.get(self.montar_url_arquivo(link), stream=True) | |
if r.status_code == 200: | |
with open(self.montar_nome_arquivo(estacao), 'wb') as f: | |
r.raw.decode_content = True | |
shutil.copyfileobj(r.raw, f) | |
print '** %s ** (baixado)' % (estacao, ) | |
else: | |
print '** %s ** (problema)' % (estacao, ) | |
def obter_link_arquivo(self, response): | |
soup = BeautifulSoup(response.content) | |
return soup.find('a', href=re.compile('^ARQ/'))['href'] | |
def executar(self): | |
post_data = {'cboTipoReg': '10'} | |
for est in self.estacoes: | |
print '** %s **' % (est, ) | |
r = requests.post(self.montar_url_estacao(est), data=post_data) | |
link = self.obter_link_arquivo(r) | |
self.salvar_arquivo_texto(est, link) | |
print '** %s ** (concluído)' % (est, ) | |
if __name__ == '__main__': | |
estacoes = ['2851050', '2751025', '2849035', '2750004', '2650032', | |
'2850015', ] | |
hid = Hidroweb(estacoes) | |
hid.executar() |
Consegui, muito bom! Parabéns! Você poupou horas de trabalho de muitas pessoas.
https://gist.github.com/58110021276b27d65e89.git
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bom dia Arthur, estou tentando utilizar esse código, mas infelizmente não estou conseguindo. Ele baixa somente os dados da primeira estação.