Advertisement
noam76

scrap php.php

May 11th, 2023
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2. require_once 'simple_html_dom.php';
  3. require_once 'PHPExcel.php';
  4.  
  5. $url = 'https://www.duurzaamloket.nl/SolKey_X014/index.php?SchemeNo=0&Offset=1&SearchText=&PageCnt=500';
  6.  
  7. // Load the HTML content from the URL
  8. $html = file_get_html($url);
  9.  
  10. // Find the table and extract the data
  11. $table = $html->find('table.LicDetails', 0);
  12. $headers = array();
  13. $rows = array();
  14. foreach ($table->find('tr') as $row) {
  15.     $cols = array();
  16.     foreach ($row->find('th,td') as $col) {
  17.         $cols[] = $col->plaintext;
  18.     }
  19.     if (empty($headers)) {
  20.         $headers = $cols;
  21.     } else {
  22.         $rows[] = $cols;
  23.     }
  24. }
  25.  
  26. // Create a new Excel workbook and write the data
  27. $wb = new PHPExcel();
  28. $ws = $wb->getActiveSheet();
  29.  
  30. // Write headers to the first row
  31. foreach ($headers as $col_num => $header) {
  32.     $ws->setCellValueByColumnAndRow($col_num, 1, $header);
  33. }
  34.  
  35. // Write table data to the worksheet
  36. foreach ($rows as $row_num => $row) {
  37.     foreach ($row as $col_num => $col) {
  38.         $ws->setCellValueByColumnAndRow($col_num, $row_num + 2, $col);
  39.     }
  40. }
  41.  
  42. // Save the workbook to an Excel file
  43. $objWriter = PHPExcel_IOFactory::createWriter($wb, 'Excel2007');
  44. $objWriter->save('solkey_data.xlsx');
  45.  
  46. echo 'Data saved to solkey_data.xlsx';
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement