Skip to content

Instantly share code, notes, and snippets.

@becahp
Created August 5, 2020 17:30
Show Gist options
  • Save becahp/61128c6d452b238447cd77abc9f74d08 to your computer and use it in GitHub Desktop.
Save becahp/61128c6d452b238447cd77abc9f74d08 to your computer and use it in GitHub Desktop.
Busca CEP - Lat, Lon.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Busca CEP - Lat, Lon.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPN0/1clj5eNcrBo2M31Pid",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/becahp/61128c6d452b238447cd77abc9f74d08/busca-cep-lat-lon.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CJS1uvkA4buE",
"colab_type": "code",
"colab": {}
},
"source": [
"import requests\n",
"import time"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "VdawF2Rw4im5",
"colab_type": "code",
"colab": {}
},
"source": [
"url = \"https://www.cepaberto.com/api/v3/cep?cep=01001000\"\n",
"# O seu token está visível apenas pra você\n",
"headers = {'Authorization': 'Token token=xxxxxxxxxxxxxxxxxxxxxxxx'}\n",
"response = requests.get(url, headers=headers)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "WYmAEKqF4jxf",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "52226312-05fd-4fd6-faac-c88bcbb30435"
},
"source": [
"print(response.json())"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"{'altitude': 760.0, 'cep': '01001000', 'latitude': '-23.5479099981', 'longitude': '-46.636', 'logradouro': 'Praça da Sé', 'bairro': 'Sé', 'complemento': '- lado ímpar', 'cidade': {'ddd': 11, 'ibge': '3550308', 'nome': 'São Paulo'}, 'estado': {'sigla': 'SP'}}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "F6y56Lg74lRn",
"colab_type": "code",
"colab": {}
},
"source": [
"url_base = \"https://www.cepaberto.com/api/v3/cep?cep=\""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "yfBiRvrk4q6W",
"colab_type": "code",
"colab": {}
},
"source": [
"lista_cep = [\"70610-900\",\n",
"\"70770-504\",\n",
"\"70070946\",\n",
"\"20021-200\",\n",
"\"73062316\",\n",
"\"70910-900\",\n",
"\"71050-175\",\n",
"\"72415110\",\n",
"\"70770-504\"]"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "OxRVRBeu48G2",
"colab_type": "code",
"colab": {}
},
"source": [
"def limpa_cep(cep):\n",
" return str(cep).replace(\"-\",\"\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "lIyy9KaI5KZw",
"colab_type": "code",
"colab": {}
},
"source": [
"ceps_limpos = [limpa_cep(i) for i in lista_cep]\n",
"ceps_limpos = set(ceps_limpos) #remover duplicados"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QASsV5Ob5PwF",
"colab_type": "code",
"colab": {}
},
"source": [
"save = {}"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "FrQWlKmQ54lQ",
"colab_type": "code",
"colab": {}
},
"source": [
"#print(\"[\" + str(response.json()['latitude']) + \", \" +str(response.json()['longitude'])+ \"]\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "TZo0fRfA5QLc",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 343
},
"outputId": "8281d90e-59f3-432c-f2dc-ff738b3b8766"
},
"source": [
"for cep in ceps_limpos:\n",
" print(\"Procurando \" + str(cep))\n",
" url_rq = url_base + cep\n",
" response = requests.get(url_rq, headers=headers)\n",
" dict_temp = response.json()\n",
" dict_temp['GEOJson'] = \"[\" + str(response.json()['latitude']) + \", \" +str(response.json()['longitude'])+ \"]\"\n",
" print(dict_temp)\n",
" save[cep] = dict_temp\n",
" time.sleep(2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Procurando 70610900\n",
"{'altitude': 1138.1, 'cep': '70610900', 'latitude': '-15.7733366', 'longitude': '-47.9042008', 'logradouro': 'SPO Área Especial 2-A', 'bairro': 'Setores Complementares', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.7733366, -47.9042008]'}\n",
"Procurando 70770504\n",
"{'altitude': 1037.8, 'cep': '70770504', 'latitude': '-15.7427357', 'longitude': '-47.8871285', 'logradouro': 'Quadra SEPN 515 Bloco D', 'bairro': 'Asa Norte', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.7427357, -47.8871285]'}\n",
"Procurando 70070946\n",
"{'altitude': 1063.2, 'cep': '70070946', 'latitude': '-15.825828', 'longitude': '-47.9057263', 'logradouro': 'SAUS Quadra 2 Bloco O', 'bairro': 'Asa Sul', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.825828, -47.9057263]'}\n",
"Procurando 20021200\n",
"{'altitude': 5.1, 'cep': '20021200', 'latitude': '-22.9040219', 'longitude': '-43.1701817', 'logradouro': 'Praça Marechal Âncora', 'bairro': 'Centro', 'cidade': {'ddd': 21, 'ibge': '3304557', 'nome': 'Rio de Janeiro'}, 'estado': {'sigla': 'RJ'}, 'GEOJson': '[-22.9040219, -43.1701817]'}\n",
"Procurando 73062316\n",
"{'altitude': 1091.2, 'cep': '73062316', 'latitude': '-15.7942287', 'longitude': '-47.8821658', 'logradouro': 'Conjunto AR 13 Conjunto 16', 'bairro': 'Setor Oeste (Sobradinho II)', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.7942287, -47.8821658]'}\n",
"Procurando 70910900\n",
"{'altitude': 1040.9, 'cep': '70910900', 'latitude': '-15.7628133161', 'longitude': '-47.8704335022', 'logradouro': 'Campus Universitário Darcy Ribeiro', 'bairro': 'Asa Norte', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.7628133161, -47.8704335022]'}\n",
"Procurando 71050175\n",
"{'altitude': 1095.3, 'cep': '71050175', 'latitude': '-15.8386975', 'longitude': '-47.9754283', 'logradouro': 'Quadra EQ 17/19', 'bairro': 'Guará II', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.8386975, -47.9754283]'}\n",
"Procurando 72415110\n",
"{'altitude': 1100.1, 'cep': '72415110', 'latitude': '-16.0232941', 'longitude': '-48.0797643', 'logradouro': 'Quadra Quadra 2 Conjunto J', 'bairro': 'Setor Sul (Gama)', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-16.0232941, -48.0797643]'}\n",
"Procurando 70770504\n",
"{'altitude': 1037.8, 'cep': '70770504', 'latitude': '-15.7427357', 'longitude': '-47.8871285', 'logradouro': 'Quadra SEPN 515 Bloco D', 'bairro': 'Asa Norte', 'cidade': {'ddd': 61, 'ibge': '5300108', 'nome': 'Brasília'}, 'estado': {'sigla': 'DF'}, 'GEOJson': '[-15.7427357, -47.8871285]'}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ix8E7HeX7pVL",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 153
},
"outputId": "1f25c9f7-cbea-4563-90b5-6949f2a95f2f"
},
"source": [
"for key, value in save.items():\n",
" print(key, '->', value['GEOJson'])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"70610900 -> [-15.7733366, -47.9042008]\n",
"70770504 -> [-15.7427357, -47.8871285]\n",
"70070946 -> [-15.825828, -47.9057263]\n",
"20021200 -> [-22.9040219, -43.1701817]\n",
"73062316 -> [-15.7942287, -47.8821658]\n",
"70910900 -> [-15.7628133161, -47.8704335022]\n",
"71050175 -> [-15.8386975, -47.9754283]\n",
"72415110 -> [-16.0232941, -48.0797643]\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment