Created
December 1, 2017 14:25
-
-
Save findcep/0e3314e46f738dfa84c353a3faadc855 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import requests | |
API_ADDRESS_BASE_URL = 'https://api.findcep.com/v1/endereco/{uf}/{cidade}/{endereco}' | |
HEADERS = {'Referer': 'https://www.your-site.com'} | |
def validate_field(field): | |
''' normalize string with replace of all spaces or tabs for "+" ''' | |
return "+".join(field.split()) | |
UF = validate_field('rj') | |
CIDADE = validate_field('rio de janeiro') | |
ENDERECO = validate_field('avenida brasil') | |
address_request = requests.get(API_ADDRESS_BASE_URL.format(uf=UF, cidade=CIDADE, endereco=ENDERECO), headers=HEADERS) | |
""" | |
Validating if search found any result, | |
if address_request.json().get('hits').get('total') > 0 return a list of address | |
Otherwise will returns an empty list | |
""" | |
print(address_request.json()['hits']['hits']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment