這是一個可以獲取網頁的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> m.survivalescaperooms.com
- </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);
- }//開源代碼Vevb.com
- }
- }
- 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有時會失敗,沒關系重試幾次即可。
新聞熱點
疑難解答