Created
September 1, 2015 17:59
-
-
Save magnunleno/b19216d2b8cee3fd216c to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import requests | |
from BeautifulSoup import BeautifulSoup | |
def sft_api(): | |
primeiro_processo = 1473119 | |
base_url = 'http://www.stf.jus.br/portal/processo/verProcessoAndamento.asp?incidente=%s' % primeiro_processo | |
page = requests.get(base_url) | |
if page.status_code != 200: | |
raise Exception, "Servidor do STF indisponível" | |
bs = BeautifulSoup(page.content) | |
dados = bs.find("div", {"id": "detalheProcesso"}) | |
if not dados: | |
raise Exception, "Processo '{}' indexistente!".format(primeiro_processo) | |
processo = {} | |
processo['titulo'] = dados.find("h3").text | |
tabela = dados.find("table") | |
for linha in tabela.findAll("tr"): | |
colunas = linha.findAll('td') | |
if not colunas: | |
continue | |
key = colunas[0].text.lower() | |
if key.endswith(":"): | |
key = key[:-1] | |
value = colunas[1].text | |
processo[key] = value | |
andamentos = [] | |
tabela = bs.find("table", {"class": "resultadoAndamentoProcesso"}) | |
if tabela: | |
for linha in tabela.findAll("tr"): | |
colunas = linha.findAll('td') | |
if not colunas: | |
continue | |
andamentos.append(map(lambda x:x.text, colunas)) | |
processo['andamentos'] = andamentos | |
return processo | |
if __name__ == '__main__': | |
try: | |
import json | |
print(json.dumps(sft_api())) | |
except ImportError as e: | |
print("Erro ao importar json!", e) | |
else: | |
print("OoOoOoOps!!!! Alguma coisa aconteceu!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment