php_采集网页_基准3步

WBOY
Release: 2016-06-13 11:53:39
Original
1004 people have browsed it

php_采集网页_标准3步

<?phpfunction 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('//','',$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;$iload($content);//从字符串中加载文档					$arr_A=$html->find($rule);//返回对象数组					foreach($arr_A as $item){						$href=$item->href;						//echo $href."";						$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 "字段收集完毕";						exit;					}				}else{					$num=$start;				}				$url=$dir_url.$num.'.htm';//构造目标url 如http://localhost/csdn/temp/1.htm				$content=file_get_contents($url);				//准备2维数组接收,首个参数是字段名,				//第2是通过正则和DOM从$content中匹配出来的各个字段的值				$preg_arr[][]=null;									require_once("simple_html_dom.php");				$html = new simple_html_dom();								//第1个字段,匹配a_title				$html->load($content);				$fields['a_title']=trim($html->find('span.link_title',0)->plaintext);				$html->clear();//千万记得清理一下内存				//var_dump($fields);exit;//sg_调试												//第2个字段,匹配a_body				$html->load($content);				$fields['a_body']=trim($html->find('div#article_content',0)->plaintext);				$html->clear();//千万记得清理一下内存				//var_dump($fields);exit;//sg_调试								//第3个字段,匹配a_pubdate				$html->load($content);				$temp=trim($html->find('span.link_postdate',0)->plaintext);				//echo $temp;//2014-01-08 17:32				$temp=strtotime($temp);				//echo date("Y n j G i s",$time);				$fields['a_pubdate']=$temp;				$html->clear();//千万记得清理一下内存				//var_dump($fields);exit;//sg_调试												//下面就可以插入数据库了				$fields["t_id"]=4;				$database='ci';				$host='localhost';				$user='root';				$pwd='root';				$table='csdn_article';				$link=mysql_connect($host,$user,$pwd);				mysql_select_db($database);				mysql_query("set names utf8");				$sql='insert into '.$table.'(t_id,a_title,a_body,a_pubdate) values("'.addslashes($fields["t_id"]).'","'.addslashes($fields["a_title"]).'","'.addslashes($fields["a_body"]).'","'.addslashes($fields["a_pubdate"]).'")';				//var_dump($sql);exit;//sg_调试				$b=mysql_query($sql) or die("Invalid query: " . mysql_error());				var_dump($b);												 $num=$num+1;				 //下面开始收集第2页 到第225页;千万注意要提交到本页面				 //basename(__FILE__);就个就是本文件的名字,带后缀				 echo '<meta http-equiv="refresh" content="0;url='.basename(__FILE__).'?num='.$num.'">';							}			}		//$a=new caiji();	//========================第1大步==========================	//$data['fenye_url']="http://blog.csdn.net/u012576807/article/list/(*)";	//$data['fenye_from']=1;  //从多少开始	//$data['fenye_to']=2;		//到多少	//$data['fenye_increment']=1;		//每次增加多少	//$fenye_list=$a->fenyelist($data);//第一阶段完成	//var_dump($fenye_list);exit;	//下面开始第2阶段,遍历数组$fenye_list列表页(http://movie.douban.com/top250?start=25&filter=&type=";)	//2选其1,根据正则,获取每个文章的具体url,返回数组$article_arr	//$reg='/http:\/\/blog\.csdn\.net\/pre_eminent\/article\/details\/[\d]*/';	//$article_arr=$a->archivesByReg($fenye_list,$reg);		//2选其1,根据DOM规则如UL里面的LI,获取每个文章的具体url存放到数组$article_arr	//$rule='div#article_list span.link_title a';	//$article_arr=$a->archivesByDOM($fenye_list,$rule);		//var_dump($article_arr);	exit;	//先序列化,要用的时候,再反序列化	//$filename="csdn";	//$filename="D:/article_arr_".$filename.".txt";	//file_put_contents($filename,serialize($article_arr));exit;	//至此,第2阶段完成,请open下一个PHP页面第3阶段,遍历数组$article_arr,本地化每一篇文章	//下面进入第4阶段,通过refresh,从本地读取每篇文章	//并取出相应字段,拼装成SQL,插入数据库	//========================第2大步==========================	//上接getAllArticles1.php;	//现在开始第3阶段,反序列化并遍历数组$article_arr,本地化每一篇文章的正文内容	/*	$filename="csdn";	$filename="D:/article_arr_".$filename.".txt";	$bodyRule='div#article_details';//这就文章的主要内容	$outputdir='D:/www_local/csdn/temp/';	$a=new caiji();	$a->downEveryArticle($filename,$bodyRule,$outputdir);exit;	*/	//========================第3大步==========================	//下面进入第4阶段,通过DOM或正则+refresh+sql插入,从本地读取每篇文章	//并取出相应字段,拼装成SQL,插入数据库	//$a->insertSQL();//注意,这个要单独拿出来!			$dir_url="http://localhost/csdn0/temp/";				set_time_limit(0);				$start=1;//如果没有传参数Num就从1开始,传了就从指定的参数开始				$end=76;				if(isset($_GET['num'])){					$num=$_GET['num'];					if($num>$end){						echo "字段收集完毕";						exit;					}				}else{					$num=$start;				}				$url=$dir_url.$num.'.htm';//构造目标url 如http://localhost/csdn/temp/1.htm				$content=file_get_contents($url);				//准备2维数组接收,首个参数是字段名,				//第2是通过正则和DOM从$content中匹配出来的各个字段的值				$preg_arr[][]=null;									require_once("simple_html_dom.php");				$html = new simple_html_dom();								//第1个字段,匹配a_title				$html->load($content);				$fields['a_title']=trim($html->find('span.link_title',0)->plaintext);				$html->clear();//千万记得清理一下内存				//var_dump($fields);exit;//sg_调试												//第2个字段,匹配a_body				$html->load($content);				$fields['a_body']=trim($html->find('div#article_content',0)->plaintext);				$html->clear();//千万记得清理一下内存				//var_dump($fields);exit;//sg_调试								//第3个字段,匹配a_pubdate				$html->load($content);				$temp=trim($html->find('span.link_postdate',0)->plaintext);				//echo $temp;//2014-01-08 17:32				$temp=strtotime($temp);				//echo date("Y n j G i s",$time);				$fields['a_pubdate']=$temp;				$html->clear();//千万记得清理一下内存				//var_dump($fields);exit;//sg_调试												//下面就可以插入数据库了				$fields["t_id"]=4;				$database='ci';				$host='localhost';				$user='root';				$pwd='root';				$table='csdn_article';				$link=mysql_connect($host,$user,$pwd);				mysql_select_db($database);				mysql_query("set names utf8");				$sql='insert into '.$table.'(t_id,a_title,a_body,a_pubdate) values("'.addslashes($fields["t_id"]).'","'.addslashes($fields["a_title"]).'","'.addslashes($fields["a_body"]).'","'.addslashes($fields["a_pubdate"]).'")';				//var_dump($sql);exit;//sg_调试				$b=mysql_query($sql) or die("Invalid query: " . mysql_error());				var_dump($b);												 $num=$num+1;				 //下面开始收集第2页 到第225页;千万注意要提交到本页面				 //basename(__FILE__);就个就是本文件的名字,带后缀				 echo '<meta http-equiv="refresh" content="0;url='.basename(__FILE__).'?num='.$num.'">';?></a>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!