Advertisement
walkiriaapps

Script listados python

Mar 12th, 2022 (edited)
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import csv
  4.  
  5. j = 1
  6. #companyIndex = 0;
  7. for j in range(1,51):
  8. if j == 1:
  9. URL = "https://www.informa.es/directorio-empresas/Comunidad_NAVARRA.html"
  10. else:
  11. URL = "https://www.informa.es/directorio-empresas/Comunidad_NAVARRA/Empresas-"+str(j)+".html#empresa"
  12.  
  13. print(URL)
  14. page = requests.get(URL)
  15. soup = BeautifulSoup(page.content, "html.parser")
  16. table = soup.find("table", class_="empresas_directorio")
  17. rows = table.find_all("tr")
  18. i = 0;
  19. for row in rows:
  20. if i != 0:
  21. #print(row)
  22. companies = {}
  23. name = row.find("td", class_="nom_empresa").find("a").text.strip()
  24. url = row.find("td", class_="nom_empresa").find("a").attrs['href']
  25.  
  26. companies[0] = url
  27. #print(companies)
  28. #print ("------------------------------")
  29. csv_columns_register = ['url']
  30. with open('urls_empresas_navarra.csv', 'a', newline='') as csvfile:
  31. w = csv.DictWriter(csvfile, companies.keys())
  32. w.writerow(companies)
  33. i +=1
  34. j+=1
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement