本文实例讲述了PHP采集静态页面并把页面css,img,js保存的方法。分享给大家供大家参考。具体分析如下:
这是一个可以获取网页的html代码以及css,js,font和img资源的小工具,主要用来快速获取模板,如果你来不及设计UI或者看到不错的模板,则可以使用这个工具来抓取网页和提
4本文¥来源gao!%daima.com搞$代*!码$网9
搞代gaodaima码
取资源文件,提取的内容会按相对路径来保存资源,因此你不必担心资源文件的错误url导入.
首页 index.php,代码如下:
<br /> <br /> <br /><meta name="author" content="flute" /> <br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <br /><title>网页抓取器</title> <br /><link rel="stylesheet" href="main.css" media="all" /> <br /><script type="text/javascript" src="jquery.js"></script> <br /><script type="text/javascript" src="main.js"></script> <br /> <br /><body> <br /><h1>Web Grabber</h1> <br /><hr /> <br /><div class="box"> <br /> <h2>Url</h2> <br /> <div class="form"> <br /> <br /> <br /> <button class="submit" type="button">Get</button><span id="tip"></span> <br /> </div><br /></div> <br /><div class="box"> <br /> <span class="all" id="saveall">Save All</span> <br /> <h2>List</h2> <br /> <ul id="list"> <br /> </ul> <br /></div> <br /> <br />
抓取页面代码 grab.php,代码如下:
<?PHP <br /> /* <br /> * flute <br /> * 2014/03/31 <br /> */ <br /> <br /> if(isset($_POST['url'])) { <br /> if(isset($_POST['project']) && !is_dir($_POST['project'])) mkdir($_POST['project'], 0777); <br /> echo json_encode(grab($_POST['url'])); <br /> } <br /> <br /> function grab($url) { <br /> //$url = 'http://ldixing-wordpress.stor.sinaapp.com/uploads/leaves/test.html'; <br /> $data = array(); <br /> $file = preg_replace('/^.*//', '', $url); <br /> <br /> if(($content = file_get_contents($url)) !== false) { <br /> <br /> if(isset($_POST['project'])) file_put_contents($_POST['project'].'/'.$file, $content); <br /> <br /> $pattern = '/<link.*?href=('|")(.*?.css)1.*?>/i'; <br /> if(preg_match_all($pattern, $content, $matches)) { <br /> $data['css'] = $matches[2]; <br /> } <br /> <br /> $pattern = '/<script.*?src=('|")(.*?.js)1.*?>/i'; <br /> if(preg_match_all($pattern, $content, $matches)) { <br /> $data['js'] = $matches[2]; <br /> } <br /> <br /> $pattern = '//i'; <br /> if(preg_match_all($pattern, $content, $matches)) { <br /> $data['img'] = $matches[2]; <br /> } <br /> <br /> $pattern = '/url(('|"|s)(.*?)1)/i'; <br /> if(preg_match_all($pattern, $content, $matches)) { <br /> $data['src'] = $matches[2]; <br /> } <br /> } <br /> <br /> return $data; <br /> }
function vardump($obj) {
echo ‘
'; <br /> print_r($obj); <br /> echo '
‘;
}
?>
保存css,js,img等资源的页面 save.php,代码如下:
<?PHP <br /> /* <br /> * flute <br /> * 2014/03/31 <br /> */ <br /> <br /> if(isset($_POST['url']) && isset($_POST['project']) && isset($_POST['domain'])) { <br /> extract($_POST); <br /> $url = preg_replace('/?.*$/', '', $url); <br /> $file = $url; <br /> $arr = explode('/', $file); <br /> $length = sizeof($arr); <br /> $filename = $arr[$length - 1]; <br /> $root = $project; <br /> $dir = $root; <br /> <br /> if($domain == 'http') { <br /> $dir = $root.'/http'; <br /> if(!is_dir($dir)) mkdir($dir, 0777); <br /> } else { <br /> $file = $domain.'/'.$url; <br /> for($i = 0; $i < $length -1; $i++) { <br /> if(!emptyempty($arr[$i])) { <br /> $dir .= '/'.$arr[$i]; <br /> if(!is_dir($dir)) mkdir($dir, 0777); <br /> }<br /> } <br /> } <br /> if(!file_exists($dir.'/'.$filename) || filesize($dir.'/'.$filename) == 0) { <br /> $content = file_get_contents($file); <br /> file_put_contents($dir.'/'.$filename, $content); <br /> } <br /> } <br />?>
使用方法:
1. 打开index页,输入项目名和要抓取的网址,网址必须是文件名结尾,如index.html;
2. 点Get按钮,得到当前页面所有的css,js,img等资源列表;
3. 点击css链接会获取css文件中的背景资源图片,附加在列表后头;
4. 点击Save All即可保存列表中所有的文件,并按相对路径生成;
5. 如果网页上有http远程文件,将会直接保存在http文件夹下;
6. Get和Save有时会失败,没关系重试几次即可。
希望本文所述对大家的php程序设计有所帮助。