Guest User

Untitled

a guest
Feb 25th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. from time import sleep
  4.  
  5. def sopa(link):
  6. res = requests.get(link)
  7. sopa = BeautifulSoup(res.text, "lxml")
  8. blocos = sopa.findAll("tr", {"class": "nome-divergente"})
  9. return blocos
  10.  
  11. planilha = []
  12. for i in range(1,819):
  13. link = "http://www.portaltransparencia.gov.br/ceis?pagina="
  14. link = link + str(i)
  15. print(link)
  16. blocos = sopa(link)
  17. sleep(random.uniform(0.2, 10))
  18. conta = 0
  19. print(blocos)
  20. input("# This is the content of blocos")
  21. for linha in blocos:
  22. if conta > 0:
  23. cnpj = linha.find("a").text.strip()
  24. link = linha.find("href")
  25. nome = linha.find("td")[1].text.strip()
  26. tipo = linha.find("td")[2].text.strip()
  27. data = linha.find("td")[3].text.strip()
  28. nome_orgao = linha.find("td")[4].text.strip()
  29. uf = linha.find("td")[5].text.strip()
  30. dicionario = {"cnpj": cnpj, "link": link, "nome": nome, "tipo": tipo, "data": data, "nome_orgao": nome_orgao, "uf": uf}
  31. print(dicionario)
  32. planilha.append(dicionario)
  33. conta = conta + 1
  34.  
  35. http://www.portaltransparencia.gov.br/ceis?pagina=1
  36. [<tr class="nome-divergente">
  37. <td>
  38. <a href="/ceis/empresa/08307727000128">
  39. 08.307.727/0001-28
  40. </a>
  41. </td>
  42. <td> TECK SHOCK COMERCIO E SERVICO EIRELI - ME ** </td>
  43. <td>Suspensão - Lei de Licitações</td>
  44. <td>16/05/2018</td>
  45. <td>Governo do Estado do Espírito Santo (ES)</td>
  46. <td>ES</td>
  47. </tr>, <tr class="nome-divergente">
  48. <td>
  49. <a href="/ceis/empresa/05236586000101">
  50. 05.236.586/0001-01
  51. </a>
  52. </td>
  53. <td> META TERCEIRIZACOES E SERVICOS LTDA ** </td>
  54. <td>Suspensão - Legislação Estadual</td>
  55. <td>26/12/2018</td>
  56. <td>Governo do Estado da Bahia</td>
  57. <td>BA</td>
  58. </tr>, <tr class="nome-divergente">
  59. <td>
  60. <a href="/ceis/empresa/05236586000101">
  61. 05.236.586/0001-01
  62. </a>
  63. </td>
  64. <td> META TERCEIRIZACOES E SERVICOS LTDA ** </td>
  65. <td>Suspensão - Legislação Estadual</td>
  66. <td>22/08/2018</td>
  67. <td>Governo do Estado da Bahia (BA)</td>
  68. <td>BA</td>
  69. </tr>, <tr class="nome-divergente">
  70. <td>
  71. <a href="/ceis/empresa/05236586000101">
  72. 05.236.586/0001-01
  73. </a>
  74. </td>
  75. <td> META TERCEIRIZACOES E SERVICOS LTDA ** </td>
  76. <td>Inidoneidade - Legislação Estadual</td>
  77. <td></td>
  78. <td>Governo do Estado da Bahia (BA)</td>
  79. <td>BA</td>
  80. </tr>, <tr class="nome-divergente">
  81. <td>
  82. <a href="/ceis/empresa/04125082000151">
  83. 04.125.082/0001-51
  84. </a>
  85. </td>
  86. <td> PRIMEIRA OPCAO LOCACAO TRANSPORTE E TURISMO LTDA - ME ** </td>
  87. <td>Inidoneidade - Lei da ANTT e ANTAQ </td>
  88. <td>16/01/2021</td>
  89. <td>AGÊNCIA NACIONAL DE TRANSPORTES TERRESTRES</td>
  90. <td>DF</td>
  91. </tr>]
  92. # This is the content of blocos
  93.  
  94. ---------------------------------------------------------------------------
  95. KeyError Traceback (most recent call last)
  96. <ipython-input-63-dae9be7999b3> in <module>()
  97. 12 cnpj = linha.find("a").text.strip()
  98. 13 link = linha.find("href")
  99. ---> 14 nome = linha.find("td")[1].text.strip()
  100. 15 tipo = linha.find("td")[2].text.strip()
  101. 16 data = linha.find("td")[3].text.strip()
  102.  
  103. ~/Documentos/Code/knight/lib/python3.6/site-packages/bs4/element.py in __getitem__(self, key)
  104. 1009 """tag[key] returns the value of the 'key' attribute for the tag,
  105. 1010 and throws an exception if it's not there."""
  106. -> 1011 return self.attrs[key]
  107. 1012
  108. 1013 def __iter__(self):
  109.  
  110. KeyError: 1
Add Comment
Please, Sign In to add comment