search
Homephp教程php手册PHP采集静态页面并把页面css,img,js保存的方法,静态页面css

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

本文实例讲述了PHP采集静态页面并把页面css,img,js保存的方法。分享给大家供大家参考。具体分析如下:

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

首页 index.php,代码如下:

复制代码 代码如下:






网页抓取器





Web Grabber





 

Url


 

   
   
   
 



  Save All
 

List


 

     




抓取页面代码 grab.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 = '//i';
   if(preg_match_all($pattern, $content, $matches)) {
    $data['css'] = $matches[2];
   }
 
   $pattern = '//i';
   if(preg_match_all($pattern, $content, $matches)) {
    $data['js'] = $matches[2];
   }
 
   $pattern = '/PHP采集静态页面并把页面css,img,js保存的方法,静态页面css/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 '

'; <br>
  print_r($obj); <br>
  echo '
';
 }
?>
保存css,js,img等资源的页面 save.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     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程序设计有所帮助。

Statement
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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor