+ -
当前位置:首页 → 问答吧 → 监控爬虫的超级精简的类

监控爬虫的超级精简的类

时间:2011-07-12

来源:互联网

<?php

/*********************************************************************
* 超级简单的判断是否有机器人蜘蛛来抓取网页的类
* 来路: www.sgcha.cn
* 看看就行
****************************************************************/

class BotIdentifier {



protected $arrstrBotMatches;


function __construct() {



//爬虫的数组



$arrstrBots = array (

   
'googlebot'        => '/Googlebot/',

'msnbot'           => '/MSNBot/',

'yahoo'            => '/Yahoo/',

   
'baidu'
=> '/Baiduspider/'  //后面要什么自己加就是了

);



//其实就是通过变量HTTP_USER_AGENT

if( true == isset( $_SERVER['HTTP_USER_AGENT'] )) {



$this->arrstrBotMatches = preg_filter( $arrstrBots, array_fill( 1, count( $arrstrBots ), '$0' ), array( trim( $_SERVER['HTTP_USER_AGENT'] )));

}

}


function getBot() {



//判断是否为bot

return ( true == is_array( $this->arrstrBotMatches ) && 0 < count( $this->arrstrBotMatches )) ? $this->arrstrBotMatches[0] : '不是爬虫';


}



function isBot() {

return ( true == is_array( $this->arrstrBotMatches ) && 0 < count( $this->arrstrBotMatches )) ? 1 : 0;


}
}

//$_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)';

$objBot = new BotIdentifier();
echo '<b>呵呵用的什么破浏览器 </b>' . $_SERVER['HTTP_USER_AGENT'];
echo '<br><b>是否是爬虫: </b>' . $boolIsBot = $objBot->isBot();
echo '<br><b>我日,什么虫: </b>' . $strBot = $objBot->getBot();

?>

作者: 大水车   发布时间: 2011-07-12

我有的时候觉得把简单的功能封装成类也不见的就比面向过程好多少,反而更累赘了,面向过程只要设计的合理一样是易维护可复用高扩展

作者: qxhy123   发布时间: 2011-07-12