찾다
php教程php手册PHP采集静态页面并把页面下载css,img,js保存

这是一个可以获取网页的html代码以及css,js,font和img资源的小工具,主要用来快速获取模板,如果你来不及设计UI或者看到不错的模板,则可以使用这个工具来抓取网页和提取资源文件,提取的内容会按相对路径来保存资源,因此你不必担心资源文件的错误url导入.

首页 index.php,代码如下:

<!DOCTYPE html> 
	<html> 
	<head> 
	<meta name="author" content="flute" /> 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>网页抓取器</title> 
	<link rel="stylesheet" href="main.css" media="all" /> 
	<script type="text/javascript" src="jquery.js"></script> 
	<script type="text/javascript" src="main.js"></script> 
	</head> 
	<body> 
	<h1 id="Web-nbsp-Grabber">Web Grabber</h1> 
	<hr /> 
	<div class="box"> 
	  <h2 id="Url">Url</h2> 
	  <div class="form"> 
	    <input type="text" id="project" value="projectname" /> 
	    <input type="text" id="url" value="http://" size="60" /> 
	    <button class="submit" type="button">Get</button><span id="tip"></span> 
	  </div> www.phprm.com 
	</div> 
	<div class="box"> 
	  <span class="all" id="saveall">Save All</span> 
	  <h2 id="List">List</h2> 
	  <ul id="list"> 
	  </ul> 
	</div> 
	</body> 
	</html> 

抓取页面代码 grab.php,代码如下:

<?PHP 
	 /* 
	 * flute 
	 * 2014/03/31 
	 */ 
	 
	 if(isset($_POST[&#39;url&#39;])) { 
	  if(isset($_POST[&#39;project&#39;]) && !is_dir($_POST[&#39;project&#39;])) mkdir($_POST[&#39;project&#39;], 0777); 
	  echo json_encode(grab($_POST[&#39;url&#39;])); 
	 } 
	 
	 function grab($url) { 
	  //$url = &#39;http://ldixing-wordpress.stor.sinaapp.com/uploads/leaves/test.html&#39;; 
	  $data = array(); 
	  $file = preg_replace(&#39;/^.*//&#39;, &#39;&#39;, $url); 
	 
	  if(($content = file_get_contents($url)) !== false) { 
	 
	   if(isset($_POST[&#39;project&#39;])) file_put_contents($_POST[&#39;project&#39;].&#39;/&#39;.$file, $content); 
	 
	   $pattern = &#39;/<link.*?href=(&#39;|")(.*?.css)1.*/i&#39;; 
	   if(preg_match_all($pattern, $content, $matches)) { 
	    $data[&#39;css&#39;] = $matches[2]; 
	   } 
	 
	   $pattern = &#39;/<script.*?src=(&#39;|")(.*?.js)1.*/i&#39;; 
	   if(preg_match_all($pattern, $content, $matches)) { 
	    $data[&#39;js&#39;] = $matches[2]; 
	   } 
	 
	   $pattern = &#39;/<img.*?src=(&#39;|")(.*?)1.*/i&#39;; 
	   if(preg_match_all($pattern, $content, $matches)) { 
	    $data[&#39;img&#39;] = $matches[2]; 
	   } 
	 
	   $pattern = &#39;/url((http://pic2.phprm.com/2014/09/22/&#39;|"|s)(.jpg)/i&#39;; 
	   if(preg_match_all($pattern, $content, $matches)) { 
	    $data[&#39;src&#39;] = $matches[2]; 
	   } 
	  } 
	 
	  return $data; 
	 } 
	 
	 
	 function vardump($obj) { 
	  echo &#39;<pre class="brush:php;toolbar:false">&#39;; 
	  print_r($obj); 
	  echo &#39;
';   }   

保存css,js,img等资源的页面 save.php,代码如下:

<?PHP 
	 /* 
	 *  flute 
	 *  2014/03/31 
	 */ 
	 
	 if(isset($_POST[&#39;url&#39;]) && isset($_POST[&#39;project&#39;]) && isset($_POST[&#39;domain&#39;])) { 
	  extract($_POST); 
	  $url = preg_replace(&#39;/?.*$/&#39;, &#39;&#39;, $url); 
	  $file = $url; 
	  $arr = explode(&#39;/&#39;, $file); 
	  $length = sizeof($arr); 
	  $filename = $arr[$length - 1]; 
	  $root = $project; 
	  $dir = $root; 
	 
	  if($domain == &#39;http&#39;) { 
	   $dir = $root.&#39;/http&#39;; 
	   if(!is_dir($dir)) mkdir($dir, 0777); 
	  } else { 
	   $file = $domain.&#39;/&#39;.$url; 
	   for($i = 0; $i < $length -1; $i++) { 
	    if(!emptyempty($arr[$i])) { 
	     $dir .= &#39;/&#39;.$arr[$i]; 
	     if(!is_dir($dir)) mkdir($dir, 0777); 
	    }//开源代码phprm.com 
	   } 
	  } 
	  if(!file_exists($dir.&#39;/&#39;.$filename) || filesize($dir.&#39;/&#39;.$filename) == 0) { 
	   $content = file_get_contents($file); 
	   file_put_contents($dir.&#39;/&#39;.$filename, $content); 
	  } 
	 } 
	 

使用方法:

1. 打开index页,输入项目名和要抓取的网址,网址必须是文件名结尾,如index.html;

2. 点Get按钮,得到当前页面所有的css,js,img等资源列表;

3. 点击css链接会获取css文件中的背景资源图片,附加在列表后头;

4. 点击Save All即可保存列表中所有的文件,并按相对路径生成;

5. 如果网页上有http远程文件,将会直接保存在http文件夹下;

6. Get和Save有时会失败,没关系重试几次即可。

本文地址:

转载随意,但请附上文章地址:-)

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.