PHP采集静态页面并把页面css,img,js保存的方法
2015-01-24信息快讯网
这篇文章主要介绍了PHP采集静态页面并把页面css,img,js保存的方法,可以实现简单的网页抓取功能,具有一定的参考借鉴价值,需要的朋友可以参考下
本文实例讲述了PHP采集静态页面并把页面css,img,js保存的方法。分享给大家供大家参考。具体分析如下:
这是一个可以获取网页的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>
</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);
}
}
}
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程序设计有所帮助。
php使用正则表达式获取图片url的方法
php使用CURL伪造IP和来源实例详解
php+mysql实现无限分类实例详解
php截取html字符串及自动补全html标签的方法
php在linux下检测mysql同步状态的方法
php中JSON的使用与转换
php5.4以下版本json不支持不转义内容中文的解决方法
php正则匹配html中带class的div并选取其中内容的方法
PHP针对JSON操作实例分析
php的sso单点登录实现方法
js+php实现静态页面实时调用用户登陆状态的方法
php读取mssql的ntext字段返回值为空的解决方法
php查询mssql出现乱码的解决方法
VPS中使用LNMP安装WordPress教程
PHP+jquery+ajax实现即时聊天功能实例
微信公众平台消息接口校验与消息接口响应实例
php获取QQ头像并显示的方法
php实现基于微信公众平台开发SDK(demo)扩展的方法
php微信公众开发之获取周边酒店信息的方法
php天翼开放平台短信发送接口实现方法
php进行支付宝开发中return_url和notify_url的区别分析
腾讯微博提示missing parameter errorcode 102 错误的解决方法
在SAE上搭建最新wordpress的方法
php中http与https跨域共享session的解决方法
thinkphp中session和cookie无效的解决方法
ThinkPHP中使用ajax接收json数据的方法
ThinkPHP通过AJAX返回JSON的两种实现方法
ThinkPHP内置jsonRPC的缺陷分析
php提示Failed to write session data错误的解决方法
php递归json类实例
php获取CSS文件中图片地址并下载到本地的方法
ThinkPHP模版中导入CSS和JS文件的方法
php实现的css文件背景图片下载器代码
php实现压缩多个CSS与JS文件的方法
php实现的CSS更新类实例