웹사이트 크롤러후 파일 압축 다운로드 > 자료실

자료실

웹사이트 크롤러후 파일 압축 다운로드

페이지 정보

profile_image
작성자 최고관리자
댓글 0 조회 1,399회 작성일 23-08-05 10:40
작성일 23-08-05 10:40

본문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<h1>웹 페이지 크롤링 및 파일 압축 다운로드</h1>
    <form action="" method="post">
        <label for="url">크롤링할 웹 페이지 URL:</label>
 
        <input type="text" name="url" id="url" size="50" required>
 
        <button type="submit" name="submit">크롤링 및 파일 압축 다운로드</button>
    </form>
    <?php
 
function isCrawlingAllowed($url) {
    $parsedUrl = parse_url($url);
    $robotsUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . '/robots.txt';
 
    $robotsContent = @file_get_contents($robotsUrl);
    if ($robotsContent === false) {
        return true; // robots.txt 파일이 없는 경우 크롤링 허용
    }
 
    $allow = true;
    $disallowPaths = array();
    $lines = explode("\n", $robotsContent);
    foreach ($lines as $line) {
        if (strpos($line, 'Disallow:') === 0) {
            $disallowPath = trim(substr($line, strlen('Disallow:')));
            if (!empty($disallowPath)) {
                $disallowPaths[] = $disallowPath;
            }
        }
    }
 
    // 확인하려는 경로가 Disallow 경로인지 체크
    foreach ($disallowPaths as $path) {
        if (strpos($url, $path) !== false) {
            $allow = false;
            break;
        }
    }
 
    return $allow;
 
 
    }
 
    if (isset($_POST["submit"])) {
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // 클라이언트로부터 URL 또는 도메인 입력 받기
            $input = $_POST["url"];
            $url = filter_var($input, FILTER_VALIDATE_URL) ? $input : getDomain($input);
 
            // URL 유효성 검사 (URL 형식 또는 도메인 형식인지 확인)
            if (filter_var($url, FILTER_VALIDATE_URL) === false) {
                die("유효하지 않은 URL 또는 도메인입니다.");
            }
 
            // 웹 페이지 HTML 내용 가져오기
            $html = file_get_contents($url);
             
            // 웹 페이지에서 모든 링크 추출
            $linkFiles = array();
            preg_match_all('/<a\s+href="([^"]+)"/i', $html, $matches);
            foreach ($matches[1] as $linkUrl) {
                $linkFiles[] = $linkUrl;
            }
 
        // 웹 페이지에서 CSS 파일 추출
        $cssFiles = array();
        preg_match_all('/<link\s+rel="stylesheet"\s+href="([^"]+)"/i', $html, $matches);
        foreach ($matches[1] as $cssUrl) {
            $cssContent = file_get_contents($cssUrl);
            $cssFilename = basename($cssUrl);
            file_put_contents($cssFilename, $cssContent);
            $cssFiles[] = $cssFilename;
        }
 
        // 웹 페이지에서 JavaScript 파일 추출
        $jsFiles = array();
        preg_match_all('/<script\s+src="([^"]+)"/i', $html, $matches);
        foreach ($matches[1] as $jsUrl) {
            $jsContent = file_get_contents($jsUrl);
            $jsFilename = basename($jsUrl);
            file_put_contents($jsFilename, $jsContent);
            $jsFiles[] = $jsFilename;
        }
 
        // 웹 페이지에서 이미지 URL 추출
            $imageFiles = array();
            preg_match_all('/<img\s+src="([^"]+)"/i', $html, $matches);
            foreach ($matches[1] as $imageUrl) {
                // 이미지 파일 다운로드
                $imageContent = file_get_contents($imageUrl);
                $imageFilename = basename($imageUrl);
                file_put_contents($imageFilename, $imageContent);
                $imageFiles[] = $imageFilename;
            }
 
        // HTML, CSS, JS, 이미지 파일들을 Gzip 압축하여 저장
            $zipFilename = "crawled_files.zip";
 
            // ZipArchive 클래스를 사용하여 압축 파일 생성
            $zip = new ZipArchive();
            if ($zip->open($zipFilename, ZipArchive::CREATE) === true) {
                $zip->addFromString('crawled_page.html', $html);
                // 링크 파일들을 텍스트 파일로 압축 파일에 추가
                $linkText = implode(PHP_EOL, $linkFiles);
                $zip->addFromString('links.txt', $linkText);
                foreach ($cssFiles as $cssFilename) {
                    $zip->addFile($cssFilename);
                }
                foreach ($jsFiles as $jsFilename) {
                    $zip->addFile($jsFilename);
                }
                foreach ($imageFiles as $imageFilename) {
                    $zip->addFile($imageFilename);
                }
                $zip->close();
 
                // 다운로드 처리 (gzip이 아닌 zip 파일로 변경)
                header('Content-Type: application/zip');
                header('Content-Disposition: attachment; filename="' . $zipFilename . '"');
                readfile($zipFilename);
 
                // 임시 파일 삭제
                unlink('crawled_page.html');
                foreach ($cssFiles as $cssFilename) {
                    unlink($cssFilename);
                }
                foreach ($jsFiles as $jsFilename) {
                    unlink($jsFilename);
                }
                foreach ($imageFiles as $imageFilename) {
                    unlink($imageFilename);
                }
                unlink($zipFilename);
            } else {
                echo "압축 파일을 생성할 수 없습니다.";
            }
        }
    }
    ?>


추천 0 비추천 0

  • 회사 : Cginjs
  • 대표 : Cginjs
  • 주소 :
  • 메일 : admin@mysample.com
  • 사업자 등록번호 :
Copyright © Cginjs All rights reserved.
notice