PHP采集靜態(tài)頁(yè)面并把頁(yè)面css,img,js保存的方法
本文實(shí)例講述了PHP采集靜態(tài)頁(yè)面并把頁(yè)面css,img,js保存的方法。分享給大家供大家參考。具體分析如下:
這是一個(gè)可以獲取網(wǎng)頁(yè)的html代碼以及css,js,font和img資源的小工具,主要用來(lái)快速獲取模板,如果你來(lái)不及設(shè)計(jì)UI或者看到不錯(cuò)的模板,則可以使用這個(gè)工具來(lái)抓取網(wǎng)頁(yè)和提取資源文件,提取的內(nèi)容會(huì)按相對(duì)路徑來(lái)保存資源,因此你不必?fù)?dān)心資源文件的錯(cuò)誤url導(dǎo)入.
首頁(yè) index.php,代碼如下:
<html>
<head>
<meta name="author" content="flute" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>網(wǎng)頁(yè)抓取器</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>
抓取頁(yè)面代碼 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 = '/<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等資源的頁(yè)面 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 < $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. 打開(kāi)index頁(yè),輸入項(xiàng)目名和要抓取的網(wǎng)址,網(wǎng)址必須是文件名結(jié)尾,如index.html;
2. 點(diǎn)Get按鈕,得到當(dāng)前頁(yè)面所有的css,js,img等資源列表;
3. 點(diǎn)擊css鏈接會(huì)獲取css文件中的背景資源圖片,附加在列表后頭;
4. 點(diǎn)擊Save All即可保存列表中所有的文件,并按相對(duì)路徑生成;
5. 如果網(wǎng)頁(yè)上有http遠(yuǎn)程文件,將會(huì)直接保存在http文件夾下;
6. Get和Save有時(shí)會(huì)失敗,沒(méi)關(guān)系重試幾次即可。
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- html靜態(tài)頁(yè)面調(diào)用php文件的方法
- PHP生成HTML靜態(tài)頁(yè)面實(shí)例代碼
- 使用PHP實(shí)現(xiàn)生成HTML靜態(tài)頁(yè)面
- 比較詳細(xì)PHP生成靜態(tài)頁(yè)面教程
- php 靜態(tài)頁(yè)面中顯示動(dòng)態(tài)內(nèi)容
- php 生成靜態(tài)頁(yè)面的辦法與實(shí)現(xiàn)代碼詳細(xì)版
- 談PHP生成靜態(tài)頁(yè)面分析 模板+緩存+寫(xiě)文件
- js+php實(shí)現(xiàn)靜態(tài)頁(yè)面實(shí)時(shí)調(diào)用用戶登陸狀態(tài)的方法
- PHP偽靜態(tài)頁(yè)面函數(shù)附使用方法
- php生成靜態(tài)頁(yè)面并實(shí)現(xiàn)預(yù)覽功能
相關(guān)文章
JS+PHP實(shí)現(xiàn)用戶輸入數(shù)字后顯示最大的值及所在位置
這篇文章主要給大家介紹了JS+PHP實(shí)現(xiàn)用戶輸入數(shù)字后顯示最大的值及是第幾個(gè)的相關(guān)位置,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-06-06
php實(shí)現(xiàn)遞歸與無(wú)限分類(lèi)的方法
這篇文章主要介紹了php實(shí)現(xiàn)遞歸與無(wú)限分類(lèi)的方法,涉及php的遞歸操作技巧,需要的朋友可以參考下2015-02-02
php的數(shù)組與字符串的轉(zhuǎn)換函數(shù)整理匯總
以下是對(duì)php中的數(shù)組與字符串的轉(zhuǎn)換函數(shù)進(jìn)行了詳細(xì)的整理匯總,需要的朋友可以參考下2013-07-07
PHP實(shí)現(xiàn)二維數(shù)組去重功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)二維數(shù)組去重功能,涉及php針對(duì)數(shù)組的遍歷、判斷、設(shè)置等相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
PHP實(shí)現(xiàn)將Word文件保存到SQL Server數(shù)據(jù)庫(kù)
這篇文章主要介紹了如何利用PHP實(shí)現(xiàn)將上傳的Word文件保存到SQL Server數(shù)據(jù)庫(kù),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法【文字,圖片,圖文】
這篇文章主要介紹了PHP實(shí)現(xiàn)微信模擬登陸并給用戶發(fā)送消息的方法,可實(shí)現(xiàn)發(fā)送文字、圖片及圖文的功能,涉及php針對(duì)微信接口的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
php實(shí)現(xiàn)的表單驗(yàn)證類(lèi)完整示例
這篇文章主要介紹了php實(shí)現(xiàn)的表單驗(yàn)證類(lèi),結(jié)合完整實(shí)例形式分析了php封裝的表單相關(guān)的正則驗(yàn)證、字符串轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
php+mysql數(shù)據(jù)庫(kù)查詢實(shí)例
這篇文章主要介紹了php+mysql數(shù)據(jù)庫(kù)查詢的方法,實(shí)例分析了數(shù)據(jù)庫(kù)查詢的原理與完整實(shí)現(xiàn)步驟,并進(jìn)行了針對(duì)性的分析說(shuō)明,需要的朋友可以參考下2015-01-01

