noam76

solkey_scraper.py

May 11th, 2023
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. import time
  2. from bs4 import BeautifulSoup
  3. from openpyxl import Workbook
  4. from selenium import webdriver
  5.  
  6. url = 'https://www.duurzaamloket.nl/SolKey_X014/index.php?SchemeNo=0&Offset=1&SearchText=&PageCnt=500'
  7.  
  8. # Set up the Selenium WebDriver (replace the path with the path to your WebDriver)
  9. driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
  10. driver.get(url)
  11.  
  12. # Wait for the table to load
  13. time.sleep(5)
  14.  
  15. # Parse the page content with BeautifulSoup
  16. soup = BeautifulSoup(driver.page_source, 'html.parser')
  17. driver.quit()
  18.  
  19. # Find the table and extract the data
  20. table = soup.find('table', class_='LicDetails')
  21. headers = [th.get_text(strip=True) for th in table.find_all('th')]
  22. rows = table.find_all('tr')[1:]
  23.  
  24. # Create a new Excel workbook and write the data
  25. wb = Workbook()
  26. ws = wb.active
  27.  
  28. # Write headers to the first row
  29. for col_num, header in enumerate(headers, 1):
  30.     ws.cell(row=1, column=col_num, value=header)
  31.  
  32. # Write table data to the worksheet
  33. for row_num, row in enumerate(rows, 2):
  34.     cols = row.find_all('td')
  35.     for col_num, col in enumerate(cols, 1):
  36.         ws.cell(row=row_num, column=col_num, value=col.get_text(strip=True))
  37.  
  38. # Save the workbook to an Excel file
  39. wb.save('solkey_data.xlsx')
Advertisement
Add Comment
Please, Sign In to add comment