防止恶意爬虫有什么好方法吗?

如题所述

1、使用nginx的自带功能

通过对httpuseragent阻塞来实现,包括GET/POST方式的请求,以nginx为例。

拒绝以wget方式的httpuseragent,增加如下内容:

Block http user agent - wget
if ($http_user_agent ~* (Wget) ) {

return 403;
}

如何拒绝多种httpuseragent,内容如下:

if ($http_user_agent ~ (agent1|agent2|Foo|Wget|Catall Spider|AcoiRobot) ) {

return 403;
}

2、限制User-Agent字段

User-Agent字段能识别用户所使用的操作系统、版本、CPU、浏览器等信息,如果请求来自非浏览器,就能识别其为爬虫,阻止爬虫抓取网站信息。

3、限制IP或账号

根据业务需求,要求用户通过验证码后才能使用某些功能或权限。当同一IP、同一设备在一定时间内访问网站的次数,系统自动限制其访问浏览。只有在输入正确的验证码之后才能继续访问。

4、验证码拦截

在登录页等页面,添加验证码,以识别是正常流量还是恶意爬虫,也是一种基本的操作。

<script src="captcha.js?appid=xxx"></script>

<script>

kg.captcha({

// 绑定元素,验证框显示区域
bind: "#captchaBox3",

// 验证成功事务处理
success: function(e) { 

console.log(e);

document.getElementById('kgCaptchaToken').value = e['token']
},    // 验证失败事务处理
failure: function(e) {

console.log(e);
},    // 点击刷新按钮时触发
refresh: function(e) {

console.log(e);
}
});

</script>

<div id="captchaBox3">载入中 ...</div>

<input type="hidden" name="kgCaptchaToken" value="" />

温馨提示:答案为网友推荐,仅供参考
相似回答