掌握蜘蛛爬取的页面,获取一些关键信息

一般来说,站长都希望掌握蜘蛛在网站上的运动痕迹,如果你会一点PHP知识,那么这个功能就会很容易实现了。我们可以通过User-Agent数据进行识别,虽然User-Agent可以伪造,但是我们可以通过User-Agent和IP地址对真假蜘蛛的行为进行判断。

正好最近写了一个分析宝塔网站日志的zblog插件,收集了比较全面的蜘蛛特征进行判断识别,本文就讲我收集的蜘蛛特征分享给大家吧!

我们想要掌握蜘蛛爬取的页面,并把相关信息保存下来,我们先来获取一些关键信息。

1、获取当前页面URL

<?function getCurrentURL() {$protocol = $_SERVER["SERVER_PORT"] == '443'?'https://':'http://';return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}?>

2、获取User-Agent

<?php// 获取User-Agent字符串$userAgent = $_SERVER['HTTP_USER_AGENT'];?>

3、获取IP地址

<?php// 获取访问者的IP地址$ipAddress = $_SERVER['REMOTE_ADDR'];?>

4、蜘蛛爬取时间

<?php$time = time();?>

4、识别蜘蛛名称

function isbot($tmp){
//谷歌蜘蛛if(strpos($tmp, 'compatible; Googlebot/2.1') !== false){$flag = '谷歌蜘蛛';} else if(strpos($tmp, 'Googlebot-Mobile') >0){$flag = '谷歌蜘蛛';} else if(strpos($tmp, 'Googlebot-Image') >0){$flag = '谷歌图片蜘蛛';} else if(strpos($tmp, 'Mediapartners-Google') >0){$flag = '谷歌广告蜘蛛';} else if(strpos($tmp, 'Adsbot-Google') >0){$flag = '谷歌质量蜘蛛';} else if(strpos($tmp, 'Googlebot') >0){$flag = '谷歌蜘蛛';} //百度蜘蛛else if(strpos($tmp, 'Baiduspider-mobile') >0){$flag = '百度蜘蛛';} else if(strpos($tmp, 'Baidu-Thumbnail') >0){$flag = '百度图片蜘蛛';} else if(strpos($tmp, 'Baiduspider-image') >0){$flag = '百度图片蜘蛛';} else if(strpos($tmp, 'Baiduspider-news') >0){$flag = '百度新闻蜘蛛';} else if(strpos($tmp, 'Baiduspider-video') >0){$flag = '百度视频蜘蛛';} else if(strpos($tmp, 'Baidu-Transcoder') >0){$flag = '百度音乐蜘蛛';} else if(strpos($tmp, 'baiduspider-mobile-gate') >0){$flag = '百度移动蜘蛛';} else if(strpos($tmp, 'Baiduspider') >0){$flag = '百度蜘蛛';} //搜搜蜘蛛else if(strpos($tmp, 'Sosospider') >0){$flag = '搜搜蜘蛛';} else if(strpos($tmp, 'Sosoimagespider') >0){$flag = '搜搜图片蜘蛛';} //雅虎蜘蛛else if(strpos($tmp, 'Yahoo! Slurp China') !== false){$flag = '雅虎中文蜘蛛';}else if(strpos($tmp, 'Yahoo ContentMatch Crawler') !== false){$flag = '雅虎竞价蜘蛛';}else if(strpos($tmp, 'Yahoo-MMCrawler') !== false){$flag = '雅虎图片蜘蛛';}else if(strpos($tmp, 'Yahoo! Slurp') !== false){$flag = '雅虎英文蜘蛛';}//微软蜘蛛 else if(strpos($tmp, 'msnbot') !== false){$flag = '微软蜘蛛';} else if(strpos($tmp, 'msnbot-media') !== false){$flag = '微软媒体蜘蛛';} else if(strpos($tmp, 'MSNBot-Media') !== false){$flag = '微软多媒体蜘蛛';}  else if(strpos($tmp, 'MSNBot-NewsBlogs') !== false){$flag = '微软新闻及blog蜘蛛';}  else if(strpos($tmp, 'MSNBot-Academic') !== false){$flag = '微软学术蜘蛛';}  else if(strpos($tmp, 'MSNBot') !== false){$flag = '微软网页蜘蛛';} //360蜘蛛 else if(strpos($tmp, 'Sosospider') !== false){$flag = '360蜘蛛';} //有道蜘蛛else if(strpos($tmp, 'YodaoBot') !== false || strpos($tmp, 'OutfoxBot') !== false){$flag = '有道蜘蛛';} //搜狗蜘蛛else if(strpos($tmp, 'Sogou web spider') !== false || strpos($tmp, 'Sogou Orion spider') !== false){$flag = '搜狗蜘蛛';} else if(strpos($tmp, 'Sogou inst spider') !== false){$flag = '搜狗蜘蛛';}  else if(strpos($tmp, 'Sogou News Spider') !== false){$flag = '搜狗新闻蜘蛛';}  else if(strpos($tmp, 'Sogou spider2') !== false){$flag = '搜狗蜘蛛';}  else if(strpos($tmp, 'Sogou blog') !== false){$flag = '搜狗blog蜘蛛';}  else if(strpos($tmp, 'sogou spider') !== false){$flag = '搜狗蜘蛛';} //其他蜘蛛 else if(strpos($tmp, 'bingbot') !== false){$flag = '必应蜘蛛';}  else if(strpos($tmp, 'EtaoSpider') !== false){$flag = '一淘网蜘蛛';}  else if(strpos($tmp, 'Scooter') !== false){$flag = 'Altavista蜘蛛';}  else if(strpos($tmp, 'Lycos_Spider') !== false){$flag = 'Lycos蜘蛛';}  else if(strpos($tmp, 'FAST-WebCrawler') !== false){$flag = 'Alltheweb蜘蛛';}  else if(strpos($tmp, 'Slurp ASPSeek ASPSeek') !== false){$flag = 'INKTOMI蜘蛛';}  else if(strpos($tmp, 'lanshanbot') !== false){$flag = '东方网景爬虫';}  else if(strpos($tmp, 'BSpider') !== false){$flag = '日本爬虫';} else if(strpos($tmp, 'fast-webcrawler') !== false){$flag = 'fast-webcrawler';} else if(strpos($tmp, 'Gaisbot') !== false){$flag = 'Gaisbot';} else if(strpos($tmp, 'ia_archiver') !== false){$flag = 'Alexa蜘蛛';} else if(strpos($tmp, 'altavista') !== false){$flag = 'altavista爬虫';} else if(strpos($tmp, 'lycos_spider') !== false){$flag = 'Lycos蜘蛛';} else if(strpos($tmp, 'Inktomi slurp') !== false){$flag = 'Inktomi slurp';}else if(strpos($tmp, 'YandexBot') !== false){$flag = 'YandexBot';}else if(strpos($tmp, 'AhrefsBot') !== false){$flag = 'AhrefsBot';}else if(strpos($tmp, 'ezooms.bot') !== false){$flag = 'ezooms.bot';}else{$flag = NULL;}return $flag;}

6、完整案例

我们首先把获取URL和识别蜘蛛的类放在bot.class.php文件里,方便后续实例化这些类!

<?require 'bot.class.php';$userAgent = $_SERVER['HTTP_USER_AGENT'];//获取访问者头部信息,该信息由浏览器发送到服务器,有可能被人为修改。$botname = isbot($userAgent);//判断访问者是不是蜘蛛if($botname) !== NULL){$time = time();$ip = $_SERVER['REMOTE_ADDR'];$url = getCurrentURL();//蜘蛛名称$botname//===========这里你就可以把以上变量存入数据库或者文件,方便后续读取!如果你需要掌握更多信息,可以自己进行完善//===========}?>

注意:以上isbot()类包括了全网最新最全蜘蛛的特征,如果你发现还有别的新的蜘蛛,欢迎留言补充!

原创文章,作者:速盾高防cdn,如若转载,请注明出处:https://www.sudun.com/ask/78037.html

(0)
速盾高防cdn's avatar速盾高防cdn
上一篇 2024年5月28日 下午6:35
下一篇 2024年5月28日 下午6:37

相关推荐

  • 电商网站的架构技术(常见的考虑点和示例技术)

    选择电商网站的架构技术需要考虑多个因素,以下是一些常见的考虑点和示例技术:1. 前端技术:– HTML、CSS 和 JavaScript:构建网站的基本前端标记语言和样…

    问答 2024年3月24日
    0
  • 商通网络加速方案(详解)

    随着网络行业的发展,越来越多的企业和个人都开始关注网络加速方案。那么什么是网络加速方案?它能为我们带来哪些好处?今天,我们将为您解密商通网络加速方案,一款备受推崇的网络加速方案。从…

    问答 2024年4月6日
    0
  • 如何正确安装ant?

    你是否在安装ant时遇到过各种问题?是否想知道如何正确安装ant?不要担心,本文将为你一一解答。首先,让我们来了解什么是ant?接着,我们会给出详细的安装步骤。同时,还会提供常见问…

    问答 2024年3月23日
    0
  • 001008是什么意思?

    001008是什么意思?这是一个在网络行业中广为人知的数字,它的出现让人不禁好奇,究竟它有着怎样的含义?随着网络行业的不断发展,001008也在其中发挥着重要的作用。但是,关于00…

    问答 2024年3月23日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注