本案采用正文分析來處理,速度不錯,效果也還可以。直接上代碼。
php智能采集核心代碼(應該是置于函數中循環調用):
$url='';//要采集的具體頁面 $d=file_get_contents($url);//獲取內容 preg_match('/<body.*?>([.\s\S]*?)<\/body>/i',$d,$rst);//獲取body內容 $d=$rst[1]; $d=strip_tags($d,'<div><p><h1><h2><h3><h4><h5><ul><ol><dl><li><dd><dt>'); //html標簽凈化 $d=preg_replace('`(<.*?>)`i',"$1\n",$d);//標簽換行,保證去除html后的文字是單獨占一行 $str=strip_tags($d);// 去除所有html標簽 $str=preg_replace('/\n+\s+\n+/',"\n",$str);//刪除空行 $arr=explode("\n",$str);//分割成數組 $_limitCount=60; //60個字符以上 $_headEmptyLines=3; //正文前后預定空行數量 $startPos = -1; // 記錄文章正文的起始位置 foreach ($arr as $key => $value) { $value=preg_replace('/\s+| | /','',$value);//去除空格 $len=mb_strlen($value);//當前行文字長度 if($startPos==-1){ // 還沒有找到文章起始位置,需要判斷起始位置 if($len>$_limitCount){ $emptyCount=0; for ($j = $key-1; $j>0; $j--){ if (strlen($arr[$j])<60){ $emptyCount++; } else { $emptyCount = 0; } if ($emptyCount == $_headEmptyLines){ $startPos = $j + $_headEmptyLines; break; } } } } } $arr=array_slice($arr,$startPos);//取出文章開始位置及其后續內容 $endPos=-1; //結束位置 foreach ($arr as $key => $value) { $value=preg_replace('/\s+| | /','',$value); if($endPos==-1){ // 還沒有找到文章結束位置,需要判斷結束位置 $emptyEnding=0; for ($j = $key; $j<$key+$_headEmptyLines; $j++){ if (mb_strlen(preg_replace('/\s+| | /','',$arr[$j]))<30){ $emptyEnding++; } else { $emptyEnding = 0; } if ($emptyEnding == $_headEmptyLines){ $endPos = $key; break; } } } } $_html='';//拼接內容 foreach ($arr as $key => $value) { if($key<$endPos){ $value=preg_replace('/\s+| | /','',$value); $_html.=$value."\n"; } }
以下內容修改于2020-11-05,之前的代碼有js內容沒有屏蔽,閾值設置也小了,還有字符長度的計算改成了strlen,應該更合理一些。
$d=file_get_contents($url); preg_match('/<title>(.*?)<\/title>/i',$d,$rst); $title=$rst[1];//標題 preg_match('/<body.*?>([.\s\S]*?)<\/body>/i',$d,$x);//獲取body $body=$x[1];//body $body=preg_replace('/\n/','',$body); $body=preg_replace('/<script.*?>.*?<\/script>|<style.*?>.*?<\/style>/i','',$body); //采集內容 $tmp=strip_tags($body,'<div><p><h1><h2><h3><h4><h5><ul><ol><dl><li><dd><dt>'); $tmp=preg_replace('`(<.*?>)`i',"$1\n",$tmp);//標簽換行 $str=strip_tags($tmp); $str=preg_replace('/\n+\s+\n+/',"\n",$str); $arr=explode("\n",$str); $_limitCount=180; //閾值 $_headEmptyLines=3; //正文前后預定空行數量 $startPos = -1; // 記錄文章正文的起始位置 foreach ($arr as $key => $value) { $value=preg_replace('/\s+| | /','',$value); $len=strlen($value); if($startPos==-1){ // 還沒有找到文章起始位置,需要判斷起始位置 if($len>$_limitCount){ $emptyCount=0; for ($j = $key-1; $j>0; $j--){ if (strlen(preg_replace('/\s+| | /','',$arr[$j]))<$_limitCount){ $emptyCount++; } else { $emptyCount = 0; } if ($emptyCount == $_headEmptyLines){ $startPos = $j + $_headEmptyLines; break; } } } } } $arr=array_slice($arr,$startPos); $endPos=-1; //結束位置 foreach ($arr as $key => $value) { $value=preg_replace('/\s+| | /','',$value); if($endPos==-1){ // 還沒有找到文章結束位置,需要判斷結束位置 $emptyEnding=0; for ($j = $key; $j<$key+$_headEmptyLines; $j++){ if (strlen(preg_replace('/\s+| | /','',$arr[$j]))<$_limitCount){ $emptyEnding++; } else { $emptyEnding = 0; } if ($emptyEnding == $_headEmptyLines){ $endPos = $key; break; } } } } $_html='';//采集到的內容 foreach ($arr as $key => $value) { if($key<$endPos){ $value=preg_replace('/\s+| | /','',$value); $_html.=$value."\n"; } } if($_html){ $strlen=strlen($_html); $t=array( 'title'=>$title, 'cont'=>$_html, 'ed'=>1 ); if($strlen<600){ $t['ed']=-1; } show_msg($url.'采集完成'); }else{ $t=array( 'title'=>$title, 'ed'=>-1 ); show_msg($url.'已跳過'); } //保存數據 M('Siteurl')->where('url="'.$url.'"')->save($t); //采集鏈接 preg_match_all('/a.*?href=(\"|\')(.*?)(\"|\')/', $body, $y); $urls=$y[2]; foreach ($urls as $key => $value) { $value=str_ireplace($this->url,'',$value);//去除url前綴 http://www.x.com $url2=strstr($this->url,'//');//取//www.x.com,因為有的站點給定的地址就是這樣 $value=str_ireplace($url2,'',$value);//去除www.x.com $value=ltrim($value,'/');//去除前/ 因為項目輸入時沒限制是否加/ if($value=='#') continue;//跳過 空地址 if($value){ if(strstr($value,'http') || strstr($value,'tel') || strstr($value,'javascript')) continue;//跳過 外部地址 $url=$this->url.$value;//拼接url $ex=M('Siteurl')->where('url="'.$url.'"')->find(); if(!$ex){ //新增 $t=array( 'sid'=>$this->sid, 'url'=>$url ); M('Siteurl')->add($t); } } }
程序運行效果:
© 致遠 2020-11-05,原創內容,轉載請注明出錯:無需寫任何規則,智能一鍵采集網頁內容-php采集源碼