以下提供给大家的是PHP版的捕捉搜索引擎蜘蛛爬行的代码。我们可以通过生成的文本文件查看每天都有哪几种搜索引擎蜘蛛爬行站点以及爬行频率,便于大家了解蜘蛛动向。
但是,从SEO工具需求角度来说,这段代码实现的功能还是很有局限性,从我个人需求而言,我希望能体现更多的信息,比如:蜘蛛爬行的页面URL、返回的状态码、每日爬行持续多长时间等等,甚至可以汇总于数据库中便于随时查询。因此,强烈呼吁编程高手开发数据库版的捕捉蜘蛛的程序。
实现蜘蛛捕捉的PHP代码如下:
function get_naps_bot() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($useragent, 'googlebot') !== false){ return 'Googlebot'; }
if (strpos($useragent, 'msnbot') !== false){ return 'MSNbot'; }
if (strpos($useragent, 'slurp') !== false){ return 'Yahoobot'; }
if (strpos($useragent, 'baiduspider') !== false){ return 'Baiduspider'; }
if (strpos($useragent, 'sohu-search') !== false){ return 'Sohubot'; }
if (strpos($useragent, 'lycos') !== false){ return 'Lycos'; }
if (strpos($useragent, 'robozilla') !== false){ return 'Robozilla'; } return false; }
function nowtime(){ $date=date("Y-m-d.G:i:s"); return $date; }
$searchbot = get_naps_bot();
if ($searchbot) { $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); $url=$_SERVER['HTTP_REFERER']; $file="robotlog.txt"; $time=nowtime(); $data=fopen($file,"a"); fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n"); fclose($data); }
使用方法: 把以下代码粘贴到你想监控的页面php代码的 ?>之间的开头或结尾即可(也可以放在模板文件内) |