Skip to content

Instantly share code, notes, and snippets.

@findcep
Last active December 1, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save findcep/acc0229b594a97c02d0d29c0e1cfaf08 to your computer and use it in GitHub Desktop.
Save findcep/acc0229b594a97c02d0d29c0e1cfaf08 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import requests
def validate_field(field):
cep = "".join(field.replace("-"," ").split())
if cep.isdigit() and len(cep) == 8:
return cep
raise Exception('CEP "{field}" Format Invalid'.format(field=field))
CEP_SEARCH = validate_field('01001001')
API_CEP_BASE_URL = 'https://api.findcep.com/v1/cep/{cep}.json'
headers = {'Referer': 'https://your-site.com'}
cep_request = requests.get(API_CEP_BASE_URL.format(cep=CEP_SEARCH) , headers=headers)
print(cep_request.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment