给WordPress博客添加一个搜索引擎蜘蛛爬行及分析页面

在新站或网站收录有问题时,想要了解搜索引擎蜘蛛对我们WordPress博客的爬行情况,对网站进行针对性的SEO优化。

每次打开服务器端访问日志查看非常麻烦,那么建立一个蜘蛛爬行及分析页面还是很有必要的。

网上已经有很多分享 WordPress 如何建立蜘蛛爬行及分享的页面文章了,不过大部分都是一个 txt 页面,密密麻麻一大堆字符,根本就看不出来哪个是哪个,所以根据网上大神分享的代码稍稍优化后让蜘蛛爬行结果更加直观明白。

具体效果如下:

WordPress WordPress博客添加蜘蛛爬行及分享页面步骤

1、将以下代码添加当前主题的 functions.php 文件中(或添加到一个 PHP 文件中,然后在 functions.php 文件中引入该文件即可)。

//自动分析蜘蛛,tzw520.cn
make_log_file();
function make_log_file(){
//log 文件名
$filename = 'mylogs.txt';
//去除 rc-ajax 评论以及 cron 机制访问记录
if(strstr($_SERVER["REQUEST_URI"],"rc-ajax")== false
&& strstr($_SERVER["REQUEST_URI"],"wp-cron.php")== false ) {
$word .= date('mdHis',$_SERVER['REQUEST_TIME'] + 3600*8) . " ";
//访问页面
$word .= $_SERVER["REQUEST_URI"] ." ";
//协议
$word .= $_SERVER['SERVER_PROTOCOL'] ." ";
//方法,POST OR GET
$word .= $_SERVER['REQUEST_METHOD'] . " ";
//$word .= $_SERVER['HTTP_ACCEPT'] . " ";
//获得浏览器信息
$word .= getbrowser(). " ";
//传递参数
$word .= "[". $_SERVER['QUERY_STRING'] . "] ";
//跳转地址
$word .= $_SERVER['HTTP_REFERER'] . " ";
//获取 IP
$word .= getIP() . " ";
$word .= "\n";
$fh = fopen($filename, "a");
fwrite($fh, $word);
fclose($fh);
}
}
//获取 IP 地址,网上现成代码 tzw520.cn
function getIP() //get ip address
{
if (getenv('HTTP_CLIENT_IP'))
{
$ip = getenv('HTTP_CLIENT_IP');
}
else if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
else if (getenv('REMOTE_ADDR'))
{
$ip = getenv('REMOTE_ADDR');
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//获取浏览器信息,移动端,平板电脑数据还未加上。tzw520.cn
function getbrowser()
{
$Agent = $_SERVER['HTTP_USER_AGENT'];
$browser = '';
$browserver = '';
 
if(ereg('Mozilla', $Agent) && ereg('Chrome', $Agent))
{
$temp = explode('(', $Agent);
$Part = $temp[2];
$temp = explode('/', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browserver = $browserver;
$browser = 'Chrome';
}
if(ereg('Mozilla', $Agent) && ereg('Firefox', $Agent))
{
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode('/', $Part);
$browserver = $temp[2];
$temp = explode(' ', $browserver);
$browserver = $temp[0];
$browserver = $browserver;
$browser = 'Firefox';
}
if(ereg('Mozilla', $Agent) && ereg('Opera', $Agent))
{
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(')', $Part);
$browserver = $temp[1];
$temp = explode(' ', $browserver);
$browserver = $temp[2];
$browserver = $browserver;
$browser = 'Opera';
}
if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent))
{
$temp = explode('(', $Agent);
$Part = $temp[1];
$temp = explode(';', $Part);
$Part = $temp[1];
$temp = explode(' ', $Part);
$browserver = $temp[2];
$browserver = $browserver;
$browser = 'Internet Explorer';
}
if($browser != '')
{
$browseinfo = $browser.' '.$browserver;
}
else
{
$browseinfo = $_SERVER['HTTP_USER_AGENT'];
}
return $browseinfo;
}
 
function get_spider_log($atts) {
extract(shortcode_atts(array(
'text' => 'yes'),$atts));
$fh = fopen(site_url() ."/mylogs.txt", "r");
$contents = "";
while(!feof($fh)){
$contents .= fread($fh, 8080);
}
fclose($fh);
$str = "";
$showtime=date("md");
if($text == "yes") {
$str.= "<div style='background-color:#33A1C9;color:white;text-align:center;font-size:20px;height:50px;line-height:50px;'>以下为国内常用蜘蛛</div>";
}
$mytmp = array();
//google
$google = 0;
if($text == "yes")
$str.= '<li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;"> 谷歌蜘蛛</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"Googlebot\/",$text);
$google += $mytmp[0];
$str.= $mytmp[1];
$mytmp = show_spider_result($showtime,$contents,"Googlebot-Image\/",$text);
$google += $mytmp[0];
$str.= $mytmp[1];
$mytmp = show_spider_result($showtime,$contents,"Googlebot-Mobile\/",$text);
$google += $mytmp[0];
$str.= $mytmp[1];
$mytmp = show_spider_result($showtime,$contents,"Feedfetcher-Google",$text);
$google += $mytmp[0];
$str.= $mytmp[1];
 
// baidu
$baidu = 0;
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">百度蜘蛛</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"Baiduspider\/",$text);
$baidu += $mytmp[0];
$str.= $mytmp[1];
$mytmp = show_spider_result($showtime,$contents,"Baiduspider-image",$text);
$baidu += $mytmp[0];
$str.= $mytmp[1];
 
//bing
$bing = 0;
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">bingbot 蜘蛛</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"bingbot\/",$text);
$bing += $mytmp[0];
$str.= $mytmp[1];
$mytmp = show_spider_result($showtime,$contents,"msnbot-media\/",$text);
$bing += $mytmp[0];
$str.= $mytmp[1];
 
//sogou
$sogou = 0;
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">搜狗蜘蛛</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"Sogou web spider\/",$text);
$sogou += $mytmp[0];
$str.= $mytmp[1];
 
//soso
$soso = 0;
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">Soso 蜘蛛</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"Sosospider\/",$text);
$soso += $mytmp[0];
$str.= $mytmp[1];
 
if($text == "yes")
$str.= "<div style='background-color:#FA8072;color:white;text-align:center;margin:auto;font-size:20px;height:50px;line-height:50px;'>以下为垃圾蜘蛛,可屏蔽抓取</div>";
//jike
$else = 0;
if($text == "yes")
$str.= '<li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">Jike Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"JikeSpider",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
//easou
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">Easou Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"EasouSpider",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
//yisou
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">YisouSpider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"YisouSpider",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">YandexBot Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"YandexBot\/",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">Mail.RU Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"Mail.RU_Bot\/",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">AcoonBot Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"AcoonBot\/",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">Exabot Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"Exabot\/",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
if($text == "yes")
$str.= '<br><li style = "text-align:center;font-weight: bold;list-style:none;margin:20px;color:orangered;">spbot Spider</li><hr/> ';
$mytmp = show_spider_result($showtime,$contents,"spbot\/",$text);
$else += $mytmp[0];
$str.= $mytmp[1];
 
$str.= draw_canvas($google,$baidu,$bing,$sogou,$soso,$else);
return $str;
}
function show_spider_result($time,$contents,$str,$text){
$count = array();
$count[0] = preg_match_all("/".$time."\d*\s\/\S*\s.*".$str."/",$contents,$mymatches);
if($text == "yes") {
$str = preg_replace("{\\\/}","",$str);
$count[1].= "<br><p style='text-align:center;'> 蜘蛛类型:".$str.": 爬行次数:".$count[0]."</p>";
if($count[0] >0) {
$tmp = substr($mymatches[0][$count[0]-1],4,6);
$tmp = substr($tmp,0,2) .":" . substr($tmp,2,2) .":" .substr($tmp,4,2) ;
$count[1].= " <p style='text-align:center;'> 最后爬行时间:'. $tmp.'</p>";
}
}
return $count;
 
}
 
 
function draw_canvas($google,$baidu,$bing,$sogou,$soso,$else){
$tmp = $google + $baidu + $bing + $sogou + $soso + $else;
if($tmp == 0) {
return "<br><br>数据不足,无法生成分析图。<br><br>";
}
$google2 = $google*100/$tmp;
$baidu2 = $baidu*100/$tmp;
$bing2 = $bing*100/$tmp;
$sogou2 = $sogou*100/$tmp;
$soso2 = $soso*100/$tmp;
$else2 = $else*100/$tmp;
$str.= "<br><div style='border-top: 1px solid #e6e6e6;'><br>
<div style='width:auto;border-width:1px;border-style:groove;padding:15px;text-align:center;list-style:none;'><b>蜘蛛爬行分析图:</b><br>";
$str.= "日期:" . date("Y-m-d");
$str.= "<br>蜘蛛一共爬行". $tmp . "次:<br>";
$str.= "<li><span style='color:#33A1C9;'>google:". $google ."次(". intval($google2) ."%)</span></li>";
$str.= "<li><span style='color:#0033ff;'>baidu:". $baidu ."次(". intval($baidu2) ."%)</span></li>";
$str.= "<li><span style='color:#872657;'>bing:". $bing ."次(". intval($bing2) ."%)</span></li>";
$str.= "<li><span style='color:#FF9912;'>sogou:". $sogou ."次(". intval($sogou2) ."%)</span></li>";
$str.= "<li><span style='color:#FF6347;'>soso:". $soso ."次(". intval($soso2) ."%)</span></li>";
$str.= "<li><span style='color:#55aa00;'>else:". $else ."次(". (100 - intval($google2) - intval($baidu2) - intval($bing2) - intval($sogou2) - intval($soso2)) ."%)</span></li></div>";
//$str.= "<img src = 'http://chart.apis.google.com/chart?cht=p3&chco=33A1C9,0033ff,872657,FF9912,FF6347,55aa00&chd=t:".$google2 .",".$baidu2.",".$bing2.",".$sogou2.",".$soso2.",".$else2."&chs=400x200&chl=google|baidu|bing|sogou|soso|else' /></div><br>";
//这里好像应该是生成一个图表,但不知道 api 好像失效了,天真找不到可以替代的,于是就给注释了
return $str;
}
add_shortcode('spiderlogs','get_spider_log');
 
//自动分析蜘蛛结束

2、登录 WordPress 后台 >> 页面 >> 新建页面,页面名称随意即可,然后把以下代码直接放在内容中并发布页面即可。

[spiderlogs]

具体如下图所示:

未经允许不得转载:主机格调 » 给WordPress博客添加一个搜索引擎蜘蛛爬行及分析页面