搜尋
首頁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 Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境