搜索
首页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.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具