国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP采集靜態頁面并把頁面下載css,img,js保存

2024-05-04 21:49:32
字體:
來源:轉載
供稿:網友

這是一個可以獲取網頁的html代碼以及css,js,font和img資源的小工具,主要用來快速獲取模板,如果你來不及設計UI或者看到不錯的模板,則可以使用這個工具來抓取網頁和提取資源文件,提取的內容會按相對路徑來保存資源,因此你不必擔心資源文件的錯誤url導入.

首頁 index.php,代碼如下:

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta name="author" content="flute" /> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  6. <title>網頁抓取器</title> 
  7. <link rel="stylesheet" href="main.css" media="all" /> 
  8. <script type="text/javascript" src="jquery.js"></script> 
  9. <script type="text/javascript" src="main.js"></script> 
  10. </head> 
  11. <body> 
  12. <h1>Web Grabber</h1> 
  13. <hr /> 
  14. <div class="box"
  15.   <h2>Url</h2> 
  16.   <div class="form"
  17.     <input type="text" id="project" value="projectname" /> 
  18.     <input type="text" id="url" value="http://" size="60" /> 
  19.     <button class="submit" type="button">Get</button><span id="tip"></span> 
  20.   </div> m.survivalescaperooms.com 
  21. </div> 
  22. <div class="box"
  23.   <span class="all" id="saveall">Save All</span> 
  24.   <h2>List</h2> 
  25.   <ul id="list"
  26.   </ul> 
  27. </div> 
  28. </body> 
  29. </html> 

抓取頁面代碼 grab.php,代碼如下:

  1. <?PHP 
  2.  /* 
  3.  * flute 
  4.  * 2014/03/31 
  5.  */ 
  6.  
  7.  if(isset($_POST['url'])) { 
  8.   if(isset($_POST['project']) && !is_dir($_POST['project'])) mkdir($_POST['project'], 0777); 
  9.   echo json_encode(grab($_POST['url'])); 
  10.  } 
  11.  
  12.  function grab($url) { 
  13.   //$url = 'http://ldixing-wordpress.stor.sinaapp.com/uploads/leaves/test.html'; 
  14.   $data = array(); 
  15.   $file = preg_replace('/^.*//'''$url); 
  16.  
  17.   if(($content = file_get_contents($url)) !== false) { 
  18.  
  19.    if(isset($_POST['project'])) file_put_contents($_POST['project'].'/'.$file$content); 
  20.  
  21.    $pattern = '/<link.*?href=('|")(.*?.css)1.*?>/i'; 
  22.    if(preg_match_all($pattern$content$matches)) { 
  23.     $data['css'] = $matches[2]; 
  24.    } 
  25.  
  26.    $pattern = '/<script.*?src=('|")(.*?.js)1.*?>/i'; 
  27.    if(preg_match_all($pattern$content$matches)) { 
  28.     $data['js'] = $matches[2]; 
  29.    } 
  30.  
  31.    $pattern = '/<img.*?src=('|")(.*?)1.*?>/i'; 
  32.    if(preg_match_all($pattern$content$matches)) { 
  33.     $data['img'] = $matches[2]; 
  34.    } 
  35.  
  36.    $pattern = '/url(('|"|s)(.*?)1)/i'; 
  37.    if(preg_match_all($pattern$content$matches)) { 
  38.     $data['src'] = $matches[2]; 
  39.    } 
  40.   } 
  41.  
  42.   return $data
  43.  } 
  44.  
  45.  
  46.  function vardump($obj) { 
  47.   echo '<pre>'
  48.   print_r($obj); 
  49.   echo '</pre>'
  50.  } 
  51. ?> 

保存css,js,img等資源的頁面 save.php,代碼如下:

  1. <?PHP 
  2.  /* 
  3.  *  flute 
  4.  *  2014/03/31 
  5.  */ 
  6.  
  7.  if(isset($_POST['url']) && isset($_POST['project']) && isset($_POST['domain'])) { 
  8.   extract($_POST); 
  9.   $url = preg_replace('/?.*$/'''$url); 
  10.   $file = $url
  11.   $arr = explode('/'$file); 
  12.   $length = sizeof($arr); 
  13.   $filename = $arr[$length - 1]; 
  14.   $root = $project
  15.   $dir = $root
  16.  
  17.   if($domain == 'http') { 
  18.    $dir = $root.'/http'
  19.    if(!is_dir($dir)) mkdir($dir, 0777); 
  20.   } else { 
  21.    $file = $domain.'/'.$url
  22.    for($i = 0; $i < $length -1; $i++) { 
  23.     if(!emptyempty($arr[$i])) { 
  24.      $dir .= '/'.$arr[$i]; 
  25.      if(!is_dir($dir)) mkdir($dir, 0777); 
  26.     }//開源代碼Vevb.com 
  27.    } 
  28.   } 
  29.   if(!file_exists($dir.'/'.$filename) || filesize($dir.'/'.$filename) == 0) { 
  30.    $content = file_get_contents($file); 
  31.    file_put_contents($dir.'/'.$filename$content); 
  32.   } 
  33.  } 
  34. ?> 

使用方法:

1. 打開index頁,輸入項目名和要抓取的網址,網址必須是文件名結尾,如index.html;

2. 點Get按鈕,得到當前頁面所有的css,js,img等資源列表;

3. 點擊css鏈接會獲取css文件中的背景資源圖片,附加在列表后頭;

4. 點擊Save All即可保存列表中所有的文件,并按相對路徑生成;

5. 如果網頁上有http遠程文件,將會直接保存在http文件夾下;

6. Get和Save有時會失敗,沒關系重試幾次即可。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清水河县| 锦屏县| 凤翔县| 镇远县| 贞丰县| 泊头市| 海口市| 清水河县| 科技| 东乌| 左云县| 新安县| 临沧市| 凤庆县| 遂昌县| 孝义市| 那曲县| 郯城县| 余庆县| 七台河市| 酒泉市| 柘城县| 花莲县| 滕州市| 日土县| 乌苏市| 苍南县| 巴中市| 聂荣县| 兴山县| 凭祥市| 丽江市| 丹巴县| 郧西县| 墨玉县| 翁源县| 元朗区| 巴里| 和田县| 建昌县| 嘉鱼县|