wordpress折腾日记:蜘蛛分析页面

作者:tianzhenwang     发布于「wordpress心得」 - 天真网

作者:tianzhenwang

年轻,无所畏惧!

手机扫码查看

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

摘要:wordpress折腾日记:蜘蛛分析页面。如图所示,天真网有一个还算看的过眼的蜘蛛分析页面。

wordpress折腾日记:蜘蛛分析页面
wordpress折腾日记:蜘蛛分析页面

如图所示,天真网有一个还算看的过眼的蜘蛛分析页面

直观,漂亮,好吧原谅我,其实不漂亮。但直观是肯定的。

漂亮看怎么说,相比于其他蜘蛛爬行记录,算是漂亮了。

因为其他大部分是一个txt页面,密密麻麻一大堆字符,根本就看不出来那个是哪个,

这个代码是网上大神分享的,天真稍稍优化了下,现在分享给大家。其实还可以优化的更好,但你必须知道天真的html+css是真的渣。

如果有大神继续优化,还请分享我一份,23333

代码

代码也是放在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.= "

以下为国内常用蜘蛛

";
}
$mytmp = array();
//google
$google = 0;
if($text == "yes")
$str.= "

  • 谷歌蜘蛛

  • ";
    $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.= "

  • 百度蜘蛛

  • ";
    $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.= "

  • bingbot蜘蛛

  • ";
    $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.= "

  • 搜狗蜘蛛

  • ";
    $mytmp = show_spider_result($showtime,$contents,"Sogou web spider\/",$text);
    $sogou += $mytmp[0];
    $str.= $mytmp[1];

    //soso
    $soso = 0;
    if($text == "yes")
    $str.= "

  • Soso蜘蛛

  • ";
    $mytmp = show_spider_result($showtime,$contents,"Sosospider\/",$text);
    $soso += $mytmp[0];
    $str.= $mytmp[1];

    if($text == "yes")
    $str.= "

    以下为垃圾蜘蛛,可屏蔽抓取

    ";
    //jike
    $else = 0;
    if($text == "yes")
    $str.= "

  • Jike Spider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"JikeSpider",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    //easou
    if($text == "yes")
    $str.= "

  • Easou Spider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"EasouSpider",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    //yisou
    if($text == "yes")
    $str.= "

  • YisouSpider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"YisouSpider",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    if($text == "yes")
    $str.= "

  • YandexBot Spider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"YandexBot\/",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    if($text == "yes")
    $str.= "

  • Mail.RU Spider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"Mail.RU_Bot\/",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    if($text == "yes")
    $str.= "

  • AcoonBot Spider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"AcoonBot\/",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    if($text == "yes")
    $str.= "

  • Exabot Spider

  • ";
    $mytmp = show_spider_result($showtime,$contents,"Exabot\/",$text);
    $else += $mytmp[0];
    $str.= $mytmp[1];

    if($text == "yes")
    $str.= "

  • spbot Spider

  • ";
    $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].= "

    蜘蛛类型:".$str.": 爬行次数:".$count[0]."

    ";
    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].= "

    最后爬行时间:". $tmp."

    ";
    }
    }
    return $count;

    }

    function draw_canvas($google,$baidu,$bing,$sogou,$soso,$else){
    $tmp = $google + $baidu + $bing + $sogou + $soso + $else;
    if($tmp == 0) {
    return "

    数据不足,无法生成分析图。

    ";
    }
    $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.= "

    蜘蛛爬行分析图:
    ";
    $str.= "日期:" . date("Y-m-d");
    $str.= "
    蜘蛛一共爬行". $tmp . "次:
    ";
    $str.= "

  • google:". $google ."次(". intval($google2) ."%)
  • ";
    $str.= "

  • baidu:". $baidu ."次(". intval($baidu2) ."%)
  • ";
    $str.= "

  • bing:". $bing ."次(". intval($bing2) ."%)
  • ";
    $str.= "

  • sogou:". $sogou ."次(". intval($sogou2) ."%)
  • ";
    $str.= "

  • soso:". $soso ."次(". intval($soso2) ."%)
  • ";
    $str.= "

  • else:". $else ."次(". (100 - intval($google2) - intval($baidu2) - intval($bing2) - intval($sogou2) - intval($soso2)) ."%)
  • ";
    //$str.= "wordpress折腾日记:蜘蛛分析页面

    ";
    //这里好像应该是生成一个图表,但不知道api好像失效了,天真找不到可以替代的,于是就给注释了
    return $str;
    }
    add_shortcode('spiderlogs','get_spider_log');

    //自动分析蜘蛛结束

    2.然后还有第二步
    新建页面,名称自定义,把下边的代码直接放在内容中,发布即可注意把删字去掉!
    [spiderlogs删]

    然后就大功告成了!

    分享到:
    未经允许不得转载:

    作者:tianzhenwang, 转载或复制请以 超链接形式 并注明出处 天真网
    原文地址:《wordpress折腾日记:蜘蛛分析页面》 发布于2019-03-28

    评论 抢沙发

    私密评论 5 + 2 =


    wordpress折腾日记:蜘蛛分析页面

    长按图片转发给朋友

    觉得文章有用就打赏一下文章作者

    支付宝扫一扫打赏

    这绝对不是广告!

    优惠券小程序,购物更省钱!

    现在,网购已经成为了主流购物趋势,淘宝京东苏宁老牌电商,拼多多后起之秀。 但你知道其实90%的商品都有大额的优惠券么?

    了解一下
    切换注册

    登录

    忘记密码 ?

    您也可以使用第三方帐号快捷登录

    Q Q 登 录
    微 博 登 录
    切换登录

    注册