rodrigosantosbr

[Py] Get CNPJ / Pessoa Jurídica data from infoplex

Jan 11th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. import urllib
  2. from urllib.request import Request, urlopen
  3. from bs4 import BeautifulSoup
  4.  
  5. def getDadosPessoaJuridicaPortalCompras(cnpj):
  6.     URL='https://www.infoplex.com.br/perfil/cnpj/{}'.format(cnpj)
  7.     try:
  8.         page = urlopen(URL)
  9.         soup = BeautifulSoup(page, "html.parser")
  10.         data1 = soup.find(class_='info-holder')
  11.         data2 = data1.find_all('p')
  12.      
  13.         """
  14.        x => <p class="outer tabela-lista-item">Id</p>
  15.        <p class="inner" style="font-family: 'Open Sans', Arial, Helvetica, sans-serif !important;margin-bottom: 0px;padding: 8px 8px;">3611</p>
  16.        """
  17.      
  18.         cnpj = ''
  19.         razao_social = ''
  20.         nome_fantasia = ''
  21.         unidade_cadastradora = ''
  22.         natureza_juridica = ''
  23.         ramo = ''
  24.         porte = ''
  25.         cnae = ''
  26.         logradouro = ''
  27.         bairro = ''
  28.         municipio = ''
  29.         cep = ''
  30.         ativo = ''
  31.      
  32.         for x in data2:
  33.             texto = x.text.strip().lower()
  34.             if texto == 'id':
  35.                 id = x.find_next_sibling().text.strip()
  36.             elif texto == 'cnpj':
  37.                 cnpj = x.find_next_sibling().text.strip()
  38.             elif texto == 'razão social':
  39.                 razao_social = x.find_next_sibling().text.strip()
  40.             elif texto == 'nome fantasia':
  41.                 nome_fantasia = x.find_next_sibling().text.strip()
  42.             elif texto == 'unidade cadastradora':
  43.                 unidade_cadastradora = x.find_next_sibling().text.strip()
  44.                 unidade_cadastradora = unidade_cadastradora.split(':')[1].strip()
  45.             elif texto == 'natureza jurídica':
  46.                 natureza_juridica = x.find_next_sibling().text.strip()
  47.                 natureza_juridica = natureza_juridica.split(':')[1].strip()
  48.             elif texto == 'ramo do negócio':
  49.                 ramo = x.find_next_sibling().text.strip()
  50.                 ramo = ramo.split(':')[1].strip()
  51.             elif texto == 'porte da empresa':
  52.                 porte = x.find_next_sibling().text.strip()
  53.                 porte = porte.split(':')[1].strip()
  54.             elif texto == 'cnae':
  55.                 cnae = x.find_next_sibling().text.strip()
  56.                 cnae = cnae.split(':')[1].strip()
  57.             elif texto == 'logradouro':
  58.                 logradouro = x.find_next_sibling().text.strip()
  59.             elif texto == 'bairro':
  60.                 bairro = x.find_next_sibling().text.strip()
  61.             elif texto == 'município':
  62.                 municipio = x.find_next_sibling().text.strip()
  63.                 municipio = municipio.split(':')[1].strip()
  64.             elif texto == 'cep':
  65.                 cep = x.find_next_sibling().text.strip()
  66.             elif texto == 'ativo':
  67.                 ativo = x.find_next_sibling().text.strip()
  68.  
  69.         data = {
  70.             "cnpj":cnpj,
  71.             "razao_social":razao_social,
  72.             "nome_fantasia":nome_fantasia,
  73.             "unidade_cadastradora":unidade_cadastradora,
  74.             "natureza_juridica":natureza_juridica,
  75.             "ramo":ramo,
  76.             "porte":porte,
  77.             "cnae":cnae,
  78.             "logradouro":logradouro,
  79.             "bairro":bairro,
  80.             "municipio":municipio,
  81.             "cep":cep,
  82.             "ativo":ativo
  83.         }
  84.         return data
  85.     except urllib.error.HTTPError:
  86.         return {}
Advertisement
Add Comment
Please, Sign In to add comment