웹사이트 크롤러
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>웹 페이지 크롤링 및 파일 압축 다운로드</title>
</head>
<body>
<h1>웹 페이지 크롤링 및 파일 압축 다운로드</h1>
<form action="" method="post">
<label for="url">크롤링할 웹 페이지 URL:</label><br>
<input type="text" name="url" id="url" size="50" required><br>
<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 "압축 파일을 생성할 수 없습니다.";
}
}
}
?>
<h2 style='font-size:1em'><a href='https://dsclub.kr'>Produced by Tak2</a></h2>
</body>
</html>
입력한 웹사이트(링크)의 이미지,html,css,js,모든 link(link.txt 형태로 저장됨)을 압축파일로 제공합니다.
파일 다운로드링크: https://dsclub.kr/bbs/board.php?bo_table=code&wr_id=276
댓글 4개
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5336 | 1년 전 | 958 | |||
| 5335 | 1년 전 | 980 | |||
| 5334 | 1년 전 | 892 | |||
| 5333 | 1년 전 | 720 | |||
| 5332 |
바다클라우드
|
1년 전 | 3648 | ||
| 5331 |
뽕엄능브라
|
1년 전 | 1675 | ||
| 5330 | 1년 전 | 1335 | |||
| 5329 |
|
1년 전 | 2656 | ||
| 5328 | 1년 전 | 2461 | |||
| 5327 | 1년 전 | 3652 | |||
| 5326 | 1년 전 | 1553 | |||
| 5325 | JavaScript |
|
1년 전 | 692 | |
| 5324 | OS | 1년 전 | 759 | ||
| 5323 | PHP |
|
1년 전 | 900 | |
| 5322 | PHP |
|
1년 전 | 797 | |
| 5321 | PHP |
|
1년 전 | 658 | |
| 5320 | PHP |
|
1년 전 | 758 | |
| 5319 | PHP |
techstar
|
1년 전 | 806 | |
| 5318 | JavaScript |
|
1년 전 | 672 | |
| 5317 | PHP |
|
1년 전 | 617 | |
| 5316 | PHP |
|
1년 전 | 582 | |
| 5315 | PHP |
|
1년 전 | 690 | |
| 5314 | PHP |
|
1년 전 | 669 | |
| 5313 | PHP |
techstar
|
1년 전 | 769 | |
| 5312 | 정규표현식 |
|
1년 전 | 646 | |
| 5311 | JavaScript |
|
1년 전 | 713 | |
| 5310 | PHP |
|
1년 전 | 2125 | |
| 5309 | PHP |
techstar
|
1년 전 | 1614 | |
| 5308 | PHP |
techstar
|
1년 전 | 679 | |
| 5307 | node.js |
swallow
|
1년 전 | 1433 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기