当前位置:首页 »“秋了秋”个人博客 » 网络技术 » 判断请求是否是搜索引擎来路

判断请求是否是搜索引擎来路

作者:秋了秋 发表时间:2021年08月15日

先说一下为什么要判断搜索引擎来路,目前很多内容类网站都会有文章访问次数统计的功能,而这个的实现原理最好的办法是在请求文章页路由的时候就加入计数+1,然而大多数情况下请求都来自于spider(搜索引擎蜘蛛),统计这类的访问次数是没有多大意义的,我们需要把这类请求过滤掉。看了下其他网站的方法是把来路ip给过滤掉,个人认为这种是不靠谱的,需要不断维护一个ip列表,搜索引擎很有可能更换ip,换一个服务器或者换一个网络ip就会变。最好的办法是判断user-agent。


我在node服务器里面把req.header请求头打印出来,发现其实识别蜘蛛很容易的,通过署名即可过滤出来,比如


谷歌搜索引擎蜘蛛请求头

'user-agent': Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.119 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

或者

'user-agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'

一个是移动端一个是网页端的蜘蛛,不管它是哪个蜘蛛,都有一个googlebot标志,相应的


Bing搜索引擎蜘蛛请求头

'user-agent': 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'


宜搜搜索引擎蜘蛛请求头

'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 YisouSpider/5.0 Safari/537.36'


搜狗搜索引擎蜘蛛请求头

'user-agent': 'Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)',

'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25(compatible; Sogou web spider/4.0; +http://www.sogou.com/docs/help/webmasters.htm#07)'


百度搜索引擎蜘蛛请求头

'user-agent': 'Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)'


字节头条搜索引擎蜘蛛请求头

'user-agent': 'Mozilla/5.0 (Linux; Android 5.0) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; Bytespider; https://zhanzhang.toutiao.com/)'


当然还有很多其它的小众搜索引擎:

  1. 'user-agent': 'Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)'

  2. 'user-agent': 'Mozilla/5.0 (compatible; SemrushBot/7~bl; +http://www.semrush.com/bot.html)'

  3. 'user-agent': 'Mozilla/5.0 (compatible; DotBot/1.2; +https://opensiteexplorer.org/dotbot; help@moz.com)'

  4. 'user-agent': 'Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)'

  5. 'user-agent': 'Mozilla/5.0 (compatible; BLEXBot/1.0; +http://webmeup-crawler.com/)'

综上所述,判断是否是搜索引擎蜘蛛的方法也很简单:

function isSpider(agent) {
    if (typeof agent !== 'string') {
        return false;
    }
    let reg = /(Googlebot|bingbot|YisouSpider|Sogou web spider|Baiduspider|Bytespider|SemrushBot|DotBot|AhrefsBot|BLEXBot)/i;
    if (agent.match(reg)) {
        return true;
    }
    return false;
}

当然这只是一部分,也是主要的一些搜索引擎蜘蛛,如果有遗漏欢迎留言补充。

需要注意的是有些搜索引擎比较奇葩,没有给出标识符,这种没给出标识符的就忽略它吧,毕竟是少数。

4
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/34.html
目录: 网络技术标签: 搜索引擎 1848次阅读

请求播放音乐,请点击播放

登 录
点击获取验证码
还没账号?点击这里