Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require '../vendor/autoload.php';
- use GuzzleHttp\Client;
- use Php\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PHPHtmlParser\Dom;
- // Fetch the webpage content
- $client = new Client();
- $response = $client->get('https://www.duurzaamloket.nl/SolKey_X014/index.php?SchemeNo=0&Offset1&SearchText=&PageCnt=500');
- $content = (string) $response->getBody();
- // Load content into a DOMDocument
- $dom = new Dom();
- $dom->loadStr($content);
- // Find the first table on the page
- $licDetailsTable = $dom->find('table', 0);
- if ($licDetailsTable === null) {
- echo "Table not found.";
- exit;
- }
- // Create a new Spreadsheet object
- $spreadsheet = new Spreadsheet();
- // Set properties
- $spreadsheet->getProperties()->setCreator("Your Name")
- ->setLastModifiedBy("Your Name")
- ->setTitle("LicDetails Data")
- ->setSubject("LicDetails Data")
- ->setDescription("Data from the LicDetails table on the website.")
- ->setKeywords("LicDetails")
- ->setCategory("Data");
- // Set the active sheet
- $sheet = $spreadsheet->getActiveSheet();
- // Set the column titles
- $sheet->setCellValue('A1', 'Project Name');
- $sheet->setCellValue('B1', 'Project Description');
- $sheet->setCellValue('C1', 'Licenties');
- // Fill the sheet with the table data
- $rowIndex = 2;
- foreach ($licDetailsTable->find('tr') as $row) {
- $colIndex = 'A';
- foreach ($row->find('td') as $cell) {
- $sheet->setCellValue($colIndex . $rowIndex, $cell->text);
- $colIndex++;
- }
- // Get the ID from the onclick link
- $onclick = $row->getAttribute('onclick');
- preg_match('/ID\d+_(\d+)/', $onclick, $matches);
- if (isset($matches[1])) {
- $id = $matches[1];
- } else {
- // Extract the ID from the URL of the current page
- $link = $row->find('a', 0);
- if ($link === null) {
- echo "Link not found for row $rowIndex.";
- continue;
- }
- $url = $link->getAttribute('href');
- preg_match('/ID=(\d+)/', $url, $matches);
- if (isset($matches[1])) {
- $id = $matches[1];
- } else {
- echo "ID not found for row $rowIndex.";
- continue;
- }
- }
- // Fetch the webpage content for the current ID
- $response = $client->get("https://www.duurzaamloket.nl/SolKey_X014/Spec_Collector.php?ID=$id&SchemeNo=0&Offset=1&Search=&PageCnt=500#");
- $content = (string) $response->getBody();
- // Load the content into a DOMDocument
- $dom = new Dom();
- $dom->loadStr($content);
- // Find the table with class "LicDetails"
- $licDetailsTable = $dom->find('div#tblTestResults_1')->find('table.LicDetails', 0);
- if ($licDetailsTable === null) {
- echo "Table not found for ID $id.";
- continue;
- }
- // Fill the sheet with the data from the table
- $colIndex = 'D';
- foreach ($licDetailsTable->find('tr') as $row) {
- $sheet->setCellValue($colIndex . $rowIndex, $row->find('td', 1)->text);
- $colIndex++;
- }
- $rowIndex++;
- }
- // Save the file with a custom name
- $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');
- $objWriter->save('LicDetails.xlsx');
- echo "Data saved to 'LicDetails.xlsx' successfully.";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment