日期:2014-05-16  浏览次数:20613 次

php_采集网页_标准3步
<?php
function unique($arr){
	//删除数组重复元素
		if(is_array($arr[0])){
			$temarr=array_flip(array_flip($arr));
		}else{
			$temarr=array_unique($arr);
		}
		return $temarr;
}
function filterJS($str){
				//过滤script标签 问号表示懒惰匹配
				$str=preg_replace('/<script(.|\s)*?\/script>/','',$str);
				return $str;
}
function filterA($str){
				$str=preg_replace('/<a ([^>]*)>/','',$str);//过滤A标签
				$str=preg_replace('/<\/a>/','',$str);
				return $str;
}
class caiji{
			public function fenyelist($data){
					//sg_调试_仿discuz采集
					//获取分页列表数组 例如:http://movie.douban.com/top250?start=(*)&filter=&type=
					$fenye_url=$data['fenye_url'];
					$fenye_from=$data['fenye_from'];		//0
					$fenye_to=$data['fenye_to'];			//200
					$fenye_increment=$data['fenye_increment'];			//25
					//现在就可以得到一个数组
					$fenye_list=array();
					for($i=$fenye_from;$i<=$fenye_to;$i+=$fenye_increment){
						$fenye_list[]=preg_replace("/\(\*\)/",$i,$fenye_url);
					}
					return $fenye_list;
			}
			
			public function archivesByReg($fenye_list,$reg){
			//下面开始第2阶段,遍历数组$fenye_list列表页(N个http://movie.douban.com/top250?start=25&filter=&type=";)
				//并且根据指定的正则,获取每个文章的具体路径存放到数组
				//http://movie.douban.com/subject/1867345/"
				//样本:$reg='/http:\/\/movie\.douban\.com\/subject\/[\d]*/';
				$arr[][]=null;
				$i=0;
				foreach($fenye_list as $one){					
				//$one="http://localhost/ci/豆瓣电影TOP250.htm";
					$temp=file_get_contents($one);
					preg_match_all($reg,$temp,$arr[$i++]);					
				}
				//var_dump($arr);	//3维数组   [0-9][0][0-25]			
				//遍历这个3维数组,将每篇文章的网址,放到结果数组并返回
				 $one_arr=array();
				 foreach($arr as $arr2){
					 foreach($arr2[0] as $item){
						$one_arr[]=$item;
					 }
				 }
				 $one_arr =unique($one_arr); //去重复元素
				 return $one_arr;
			}

			public function archivesByDOM($fenye_list,$rule){
				require_once("../simple_html_dom.php");//在www_local根目录
				$html = new simple_html_dom();
				$one_arr=array();//准备一个空数组,接收
				foreach($fenye_list as $one){//遍历列表数组
					$content=file_get_contents($one);//取出每一个列表的HTML
					$html->load($content);//从字符串中加载文档
					$arr_A=$html->find($rule);//返回对象数组
					foreach($arr_A as $item){
						$href=$item->href;
						//echo $href."</br>";
						$one_arr[]=$href;//将每一篇文章的url加到数组当中
					}
					//千万记得清理一下内存
					$html->clear();
				}
				$one_arr =unique($one_arr); //去重复url
				 return $one_arr;
			}

			public function downEveryArticle($filename,$bodyRule,$outputdir){
				//现在开始第3阶段,反序列化filename,得到数组$article_arr,每个成员是一篇文章url
				//遍历$article_arr,利用$bodyRule本地化每一篇文章的正文内容,输出到指定目录
				$article_arr=unserialize(file_get_contents($filename));
				//var_dump($article_arr);exit;//sg_调试
				require_once("../simple_html_dom.php");//在www_local根目录
				$html = new simple_html_dom();
				$i=1;
				if(!file_exists($outputdir)){
					mkdir($outputdir);
				}
				set_time_limit(0);
				foreach($article_arr as $one){//$one对应一篇文章的Url
					// if($i>25){
						// exit;
					// }
					$file=$outputdir.$i.'.htm';
					if(!file_exists($file)){
							$content=filterJS(file_get_contents($one));
							$html->load($content);
							$content=$html->find($bodyRule,0)->innertext;
							//var_dump($content);exit;//sg_调试
							$html->clear();//千万记得清理一下内存
							//本地化每一篇文章的正文内容
							file_put_contents($file,$content);
					}
					$i=$i+1;
				}
				echo "采集文章,到本地完毕,请进入下一步,DOM或正则+refresh+sql插入";
			}

			public function insertSQL(){
				//DOM或正则采集单篇文章各个字段+sql插入+refresh
				//单篇文章已经本地化:http://localhost/csdn/temp/1.htm         num为1-76
				//先取得num,为构造目标url作准备
				$dir_url="http://localhost/csdn/temp/";
				set_time_limit(0);
				$start=1;//如果没有传参数Num就从1开始,传了就从指定的参数开始
				$end=30;
				if(isset($_GET['num'])){
					$num=$_GET['num'];
					if($num>$end){
						echo "字段收集完毕";