当前位置:首页 > 技术文档 > 正文内容

PHP采集静态页面并把页面css,img,js保存的方法

admin8年前 (2017-04-09)技术文档1697

本文实例讲述了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>Web Grabber</h1> 
<hr /> 
<div class="box"> 
  <h2>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>
</div> 
<div class="box"> 
  <span class="all" id="saveall">Save All</span> 
  <h2>List</h2> 
  <ul id="list"> 
  </ul> 
</div> 
</body> 
</html>


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


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

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


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

使用方法:


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

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

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

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

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

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

希望本文所述对大家的php程序设计有所帮助。


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:http://yuyunhe.cn/index.php/post/234.html

分享给朋友:

“PHP采集静态页面并把页面css,img,js保存的方法” 的相关文章

php 获取页面内容

function get_contents($url){     if(function_exists('file_get_contents')){       &nbs...

jQuery编程的最佳实践

加载jQuery1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢。点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地址。<script type="text/javascript" src="...

Discuz!二次开发必备知识——DZ程序文件目录含义

分类: DZ程序文件目录含义基于7.0的标准程序,部分与插件无关的文件不作说明文件颜色说明:红色:程序核心文件,修改这类文件时千万要注意安全!橙色:做插件几乎不会用到的文件,大概了解功能就可以了,其实我也不推荐修改这些文件绿色:函数类文件,许多功能强大的自定义函数可以调用这类文件来...

php利用百度地图API进行IP定位和GPS定位

最 近在做一个手机端的webapp地图应用,而核心内容当然是定位了,但是定位的话有几种方式,IP定位,GPS定位,基站定位(这个貌似webapp用不 了), 那么剩下核心的gps定位和ip定位了,我们知道,html5有定位API,但是该API拿到的GPS数据是硬件坐标,无法直接显示在地图上。...

wamp虚拟主机配置

1、首先打开apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#,启用虚拟主机功能2、先把localhost配置好,免得以后访问localhost出现问题,我的wamp项目根目录是D:\wamp\www。将下面信息添加到...

如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)

如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)?先上代码进行java时间转换成unix timestampimport java.text.DateFormat;   import java.text.ParseExceptio...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。