Table of Contents
Web Grabber
Url
List
Home php教程 php手册 PHP采集静态页面并把页面下载css,img,js保存

PHP采集静态页面并把页面下载css,img,js保存

Jun 02, 2016 am 09:13 AM

这是一个可以获取网页的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> 
Copy after login

抓取页面代码 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;
Copy after login
'; }

保存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); 
	  } 
	 } 
	 
Copy after login

使用方法:

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

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

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

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

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

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

本文地址:

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)