noam76

extract1.php

May 24th, 2023
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2. require '../vendor/autoload.php';
  3.  
  4. use GuzzleHttp\Client;
  5. use Php\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PHPHtmlParser\Dom;
  8.  
  9. // Fetch the webpage content
  10. $client = new Client();
  11. $response = $client->get('https://www.duurzaamloket.nl/SolKey_X014/index.php?SchemeNo=0&Offset1&SearchText=&PageCnt=500');
  12. $content = (string) $response->getBody();
  13.  
  14. // Load content into a DOMDocument
  15. $dom = new Dom();
  16. $dom->loadStr($content);
  17.  
  18. // Find the first table on the page
  19. $licDetailsTable = $dom->find('table', 0);
  20.  
  21. if ($licDetailsTable === null) {
  22.     echo "Table not found.";
  23.  exit;
  24. }
  25.  
  26. // Create a new Spreadsheet object
  27. $spreadsheet = new Spreadsheet();
  28.  
  29. // Set properties
  30. $spreadsheet->getProperties()->setCreator("Your Name")
  31.     ->setLastModifiedBy("Your Name")
  32.     ->setTitle("LicDetails Data")
  33.     ->setSubject("LicDetails Data")
  34.     ->setDescription("Data from the LicDetails table on the website.")
  35.     ->setKeywords("LicDetails")
  36.     ->setCategory("Data");
  37.  
  38. // Set the active sheet
  39. $sheet = $spreadsheet->getActiveSheet();
  40.  
  41. // Set the column titles
  42. $sheet->setCellValue('A1', 'Project Name');
  43. $sheet->setCellValue('B1', 'Project Description');
  44. $sheet->setCellValue('C1', 'Licenties');
  45.  
  46. // Fill the sheet with the table data
  47. $rowIndex = 2;
  48. foreach ($licDetailsTable->find('tr') as $row) {
  49.     $colIndex = 'A';
  50.     foreach ($row->find('td') as $cell) {
  51.         $sheet->setCellValue($colIndex . $rowIndex, $cell->text);
  52.         $colIndex++;
  53.     }
  54.     // Get the ID from the onclick link
  55.     $onclick = $row->getAttribute('onclick');
  56.     preg_match('/ID\d+_(\d+)/', $onclick, $matches);
  57.     if (isset($matches[1])) {
  58.         $id = $matches[1];
  59.     } else {
  60.         // Extract the ID from the URL of the current page
  61.         $link = $row->find('a', 0);
  62.         if ($link === null) {
  63.             echo "Link not found for row $rowIndex.";
  64.             continue;
  65.         }
  66.         $url = $link->getAttribute('href');
  67.         preg_match('/ID=(\d+)/', $url, $matches);
  68.         if (isset($matches[1])) {
  69.             $id = $matches[1];
  70.         } else {
  71.             echo "ID not found for row $rowIndex.";
  72.             continue;
  73.         }
  74.     }
  75.  
  76.     // Fetch the webpage content for the current ID
  77.     $response = $client->get("https://www.duurzaamloket.nl/SolKey_X014/Spec_Collector.php?ID=$id&SchemeNo=0&Offset=1&Search=&PageCnt=500#");
  78.     $content = (string) $response->getBody();
  79.  
  80.     // Load the content into a DOMDocument
  81.     $dom = new Dom();
  82.     $dom->loadStr($content);
  83.  
  84.     // Find the table with class "LicDetails"
  85.     $licDetailsTable = $dom->find('div#tblTestResults_1')->find('table.LicDetails', 0);
  86.  
  87.     if ($licDetailsTable === null) {
  88.         echo "Table not found for ID $id.";
  89.         continue;
  90.     }
  91.  
  92.     // Fill the sheet with the data from the table
  93.     $colIndex = 'D';
  94.     foreach ($licDetailsTable->find('tr') as $row) {
  95.         $sheet->setCellValue($colIndex . $rowIndex, $row->find('td', 1)->text);
  96.         $colIndex++;
  97.     }
  98.  
  99.     $rowIndex++;
  100. }
  101.  
  102. // Save the file with a custom name
  103. $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');
  104. $objWriter->save('LicDetails.xlsx');
  105.  
  106. echo "Data saved to 'LicDetails.xlsx' successfully.";
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment