• 09-10-2024, 14:11:53
    #11
    herkese teşekkürler.
  • 09-10-2024, 14:22:10
    #12

    <?php
    
    // Composer ile PhpSpreadsheet'i kullanmak için aşağıdaki satırı ekleyin
    require 'vendor/autoload.php'; // PhpSpreadsheet için gerekli
    
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    
    // 1. HTML İçeriğini URL'den Çekme Fonksiyonu
    function getHTMLContent($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // SSL doğrulamasını kapatma
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }
    
    // 2. HTML İçeriğinden İlgili Veriyi Ayıklama Fonksiyonu
    function extractTeamInfo($htmlContent) {
        $dom = new DOMDocument();
        @$dom->loadHTML($htmlContent); // Hataları gizlemek için '@' ekledik
        $xpath = new DOMXPath($dom);
    
        $data = [];
    
        // İlgili "div" içindeki bilgileri çekiyoruz
        $teams = $xpath->query("//div[contains(@class, 'team-info-text')]");
        foreach ($teams as $team) {
            $name = $xpath->query(".//h2", $team)->item(0);
            $position = $xpath->query(".//span[@class='mb-1 fs-6 fw-bolder']", $team)->item(0);
            $address = $xpath->query(".//address", $team)->item(0);
            $phone = $xpath->query(".//a[contains(@href, 'tel:')]", $team)->item(0);
            $email = $xpath->query(".//a[contains(@href, 'mailto:')]", $team)->item(0);
    
            // Null check for each field before accessing nodeValue
            $data[] = [
                'name' => $name ? trim($name->nodeValue) : 'N/A',
                'position' => $position ? trim($position->nodeValue) : 'N/A',
                'address' => $address ? trim($address->nodeValue) : 'N/A',
                'phone' => $phone ? trim($phone->nodeValue) : 'N/A',
                'email' => $email ? trim(str_replace('mailto:', '', $email->getAttribute('href'))) : 'N/A', // Extract the email without 'mailto:'
            ];
        }
    
        return $data;
    }
    
    // 3. Verileri Excel Dosyasına Yazma Fonksiyonu
    function writeToExcel($data) {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
    
        // Başlık satırı
        $sheet->setCellValue('A1', 'Name');
        $sheet->setCellValue('B1', 'Position');
        $sheet->setCellValue('C1', 'Address');
        $sheet->setCellValue('D1', 'Phone');
        $sheet->setCellValue('E1', 'Email');  // Add the email column
    
        // Verileri doldurma
        $row = 2; // 2. satırdan başlıyoruz
        foreach ($data as $item) {
            $sheet->setCellValue('A' . $row, $item['name']);
            $sheet->setCellValue('B' . $row, $item['position']);
            $sheet->setCellValue('C' . $row, $item['address']);
            $sheet->setCellValue('D' . $row, $item['phone']);
            $sheet->setCellValue('E' . $row, $item['email']);  // Write the email to the Excel sheet
            $row++;
        }
    
        // Excel dosyasını kaydetme
        $writer = new Xlsx($spreadsheet);
        $writer->save('team_info.xlsx');
        echo "Veriler başarıyla 'team_info.xlsx' dosyasına yazıldı.";
    }
    
    // 4. Tüm Sayfaları Dolaşıp Verileri Toplama ve Excel'e Yazdırma
    $allData = [];
    for ($i = 1; $i <= 32; $i++) {
        $url = "https://www.rumelisiad.org.tr/icerik/uyeler?sayfa={$i}&ara=&harf=";
        $htmlContent = getHTMLContent($url); // Sayfa içeriğini al
        $pageData = extractTeamInfo($htmlContent); // Verileri çıkar
        $allData = array_merge($allData, $pageData); // Tüm sayfalardan veriyi birleştir
    }
    
    // Verileri Excel'e yazdır
    writeToExcel($allData);


    Tüm bilgiler için son güncel hali