Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require '../vendor/autoload.php';
- use GuzzleHttp\Client;
- use PHPHtmlParser\Dom;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- // Fetch the webpage content
- $client = new Client();
- $response = $client->get('https://www.duurzaamloket.nl/SolKey_X014/index.php?SchemeNo=0&Offset=1&SearchText=&PageCnt=500');
- $content = (string) $response->getBody();
- // Load content into a DOMDocument
- $dom = new Dom();
- $dom->load($content);
- // Find all <td> elements with an ID attribute
- $tdElements = $dom->find('td[id]');
- // Create a new Excel spreadsheet
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- // Set the column headers
- $sheet->setCellValue('A1', 'ID');
- $sheet->setCellValue('B1', 'Data');
- // Extract the data from each <td> element with an ID attribute and save to the Excel file
- $rowIndex = 2;
- $idStrings = array(); // Initialize the string table
- foreach ($tdElements as $td) {
- $id = $td->getAttribute('id');
- $data = $td->text;
- // Save to file if the <td> element has an ID attribute
- if ($id !== null) {
- // Extract the number from the ID and save to the string table
- $idNumber = substr($id, 4);
- $idStrings[] = $idNumber;
- // Save to Excel file
- $sheet->setCellValue('A' . $rowIndex, $idNumber);
- $sheet->setCellValue('B' . $rowIndex, $data);
- $rowIndex++;
- }
- }
- // Save the Excel file
- $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
- $writer->save('output.xlsx');
- // Print the string table
- print_r($idStrings);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement