首页  >  文章  >  后端开发  >  php实现搜索引擎爬行代码分享

php实现搜索引擎爬行代码分享

小云云
小云云原创
2018-03-03 09:28:511590浏览

本文主要和大家介绍了php记录搜索引擎爬行记录的实现代码,然后在文中给大家补充介绍了php获取各搜索蜘蛛爬行记录的代码,需要的朋友可以参考下,希望能帮助到大家。

下面是完整代码:

//记录搜索引擎爬行记录 $searchbot = get_naps_bot(); 
if ($searchbot) 
{ $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); 
$url = $_SERVER['HTTP_REFERER']; 
$file = WEB_PATH.'robotslogs.txt'; 
$date = date('Y-m-d H:i:s'); 
$data = fopen($file,'a'); 
fwrite($data,"Time:$date robot:$searchbot URL:$tlc_thispage/r/n"); 
fclose($data);
}

WEB_PATH为index.PHP下define的根目录路径,意思就是说robotslogs.txt文件是放在根目录下的。

通过get_naps_bot()获取蜘蛛爬行记录,然后在通过addslashes处理一下,将数据存储于变量$tlc_thispage中。

fopen打开robotslogs.txt文件,将数据通过函数fwrite写入,在通过函数fclose关闭就可以了。

因为我觉得没必要,所以把自己网站上的代码删除了,所以也没有效果示例了。

PS:php获取各搜索蜘蛛爬行记录的代码

支持如下的搜索引擎:Baidu,Google,Bing,Yahoo,Soso,Sogou,Yodao爬行网站的记录!

代码:

<?php 
/**
* 获取搜索引擎爬行记录
* edit by www.jb51.net
*/
function get_naps_bot() 
{ 
$useragent = strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]); 
if (strpos($useragent, &#39;googlebot&#39;) !== false){ 
return &#39;Google&#39;; 
} 
if (strpos($useragent, &#39;baiduspider&#39;) !== false){ 
return &#39;Baidu&#39;; 
} 
if (strpos($useragent, &#39;msnbot&#39;) !== false){ 
return &#39;Bing&#39;; 
} 
if (strpos($useragent, &#39;slurp&#39;) !== false){ 
return &#39;Yahoo&#39;; 
} 
if (strpos($useragent, &#39;sosospider&#39;) !== false){ 
return &#39;Soso&#39;; 
} 
if (strpos($useragent, &#39;sogou spider&#39;) !== false){ 
return &#39;Sogou&#39;; 
} 
if (strpos($useragent, &#39;yodaobot&#39;) !== false){ 
return &#39;Yodao&#39;; 
} 
return false; 
} 
function nowtime(){ 
$date=date("Y-m-d.G:i:s"); 
return $date; 
} 
$searchbot = get_naps_bot(); 
if ($searchbot) { 
$tlc_thispage = addslashes($_SERVER[&#39;HTTP_USER_AGENT&#39;]); 
$url=$_SERVER[&#39;HTTP_REFERER&#39;]; 
$file="www.jb51.net.txt"; 
$time=nowtime(); 
$data=fopen($file,"a"); 
fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n"); 
fclose($data); 
} 
?>

相关推荐:

jQuery Jsonp跨域模拟搜索引擎实例分享

php 对现有搜索引擎的调用详解

关于JavaScript如何切换搜索引擎的导航网页搜索框的实例代码分享

以上是php实现搜索引擎爬行代码分享的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn