Home > Backend Development > PHP Tutorial > How to collect static pages in PHP and save the page css, img, js, static page css_PHP tutorial

How to collect static pages in PHP and save the page css, img, js, static page css_PHP tutorial

WBOY
Release: 2016-07-13 10:10:58
Original
866 people have browsed it

How to collect static pages in PHP and save the page css, img, js, static page css

The example in this article describes the method of collecting static pages in PHP and saving the page css, img, and js. Share it with everyone for your reference. The specific analysis is as follows:

This is a small tool that can obtain the HTML code and css, js, font and img resources of a web page. It is mainly used to quickly obtain templates. If you have no time to design the UI or see a good template, you can use this tool. Crawl web pages and extract resource files. The extracted content will save resources according to relative paths, so you don’t have to worry about incorrect URL import of resource files.

Homepage index.php, the code is as follows:

Copy code The code is as follows:





Web scraper





Web Grabber





Url









Save All

List







Grab the page code grab.php, the code is as follows:
Copy code The code is as follows:
/*
* 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 = '//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 '
';
}
?>


Page save.php that saves css, js, img and other resources, the code is as follows:
Copy code The code is as follows:
 /*
 *  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程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/932075.htmlTechArticlePHP采集静态页面并把页面css,img,js保存的方法,静态页面css 本文实例讲述了PHP采集静态页面并把页面css,img,js保存的方法。分享给大家供大家...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template