当前位置:首页 > 新闻资讯 > 建站经验

怎样禁止服务器爬虫?

发表日期:2019-05-12浏览次数:

标签: 爬虫 服务器

一、Apache

①、通过修改 .htaccess 文件

修改网站目录下的.htaccess,添加如下代码即可(2 种代码任选):

可用代码 (1):

RewriteEngineOnRewriteCond%{HTTp_USER_AGENT}(^$|FeedDemon|IndyLibrary|AlexaToolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedparser|ApacheBench|MicrosoftURLControl|Swiftbot|ZmEu|oBot|jaunty|python–urllib|lightDeckReportsBot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms)[NC]RewriteRule^(.*)$–[F]

可用代码 (2):

SetEnvIfNoCase^User–Agent$.*(FeedDemon|IndyLibrary|AlexaToolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedparser|ApacheBench|MicrosoftURLControl|Swiftbot|ZmEu|oBot|jaunty|python–urllib|lightDeckReportsBot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms)BADBOTOrderAllow,DenyAllowfromallDenyfromenv=BADBOT

②、通过修改 httpd.conf 配置文件

找到如下类似位置,根据以下代码 新增 / 修改,然后重启 Apache 即可:

Shell

DocumentRoot/home/wwwroot/xxx<Directory“/home/wwwroot/xxx”>SetEnvIfNoCaseUser–Agent“.*(FeedDemon|IndyLibrary|AlexaToolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedparser|ApacheBench|MicrosoftURLControl|Swiftbot|ZmEu|oBot|jaunty|python-urllib|lightDeckReportsBot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms)”BADBOTOrderallow,denyAllowfromalldenyfromenv=BADBOT</Directory>

怎样禁止服务器爬虫? 如何做网站挣钱

二、Nginx 代码

进入到 nginx 安装目录下的 conf 目录,将如下代码保存为 agent_deny.conf

cd/usr/local/nginx/confvimagent_deny.conf
#禁止Scrapy等工具的抓取if($http_user_agent~*(Scrapy|Curl|HttpClient)){return403;}#禁止指定UA及UA为空的访问if($http_user_agent~*“FeedDemon|IndyLibrary|AlexaToolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedparser|ApacheBench|MicrosoftURLControl|Swiftbot|ZmEu|oBot|jaunty|python-urllib|lightDeckReportsBot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$”){return403;}#禁止非GET|HEAD|pOST方式的抓取if($request_method!~^(GET|HEAD|pOST)$){return403;}

然后,在网站相关配置中的 location / { 之后插入如下代码:

Shell

includeagent_deny.conf;

如下的配置:

Shell

[marsge@Mars_Server~]$cat/usr/local/nginx/conf/zhangge.conflocation/{try_files$uri$uri//index.php?$args;#这个位置新增1行:includeagent_deny.conf;rewrite^/sitemap_360_sp.txt$/sitemap_360_sp.phplast;rewrite^/sitemap_baidu_sp.xml$/sitemap_baidu_sp.phplast;rewrite^/sitemap_m.xml$/sitemap_m.phplast;

保存后,执行如下命令,平滑重启 nginx 即可:

Shell

/usr/local/nginx/sbin/nginx–sreload

三、pHp 代码

将如下方法放到贴到网站入口文件 index.php 中的第一个

pHp

//获取UA信息$ua=$_SERVER[‘HTTp_USER_AGENT’];//将恶意USER_AGENT存入数组$now_ua=array(‘FeedDemon‘,‘BOT/0.1(BOTforJCE)’,‘CrawlDaddy‘,‘Java’,‘Feedly’,‘UniversalFeedparser’,‘ApacheBench’,‘Swiftbot’,‘ZmEu’,‘IndyLibrary’,‘oBot’,‘jaunty’,‘YandexBot’,‘AhrefsBot’,‘MJ12bot’,‘WinHttp’,‘EasouSpider’,‘HttpClient’,‘MicrosoftURLControl’,‘YYSpider’,‘jaunty’,‘python-urllib’,‘lightDeckReportsBot’);//禁止空USER_AGENT,dedecms等主流采集程序都是空USER_AGENT,部分sql注入工具也是空USER_AGENTif(!$ua){header(“Content-type:text/html;charset=utf-8”);die(‘请勿采集本站,因为采集的站长木有小JJ!’);}else{foreach($now_uaas$value)//判断是否是数组中存在的UAif(eregi($value,$ua)){header(“Content-type:text/html;charset=utf-8”);die(‘请勿采集本站,因为采集的站长木有小JJ!’);}}

四、测试效果

如果是 VpS,那非常简单,使用 curl -A 模拟抓取即可,比如:

模拟宜搜蜘蛛抓取:

Shell

curl–I–A‘YisouSpider’bizhi.bcoderss.com

模拟 UA 为空的抓取:

Shell

curl–I–A”bizhi.bcoderss.com

模拟百度蜘蛛的抓取:

Shell

curl–I–A‘Baiduspider’bizhi.bcoderss.com

如没特殊注明,文章均为AG环亚集团平台科技原创,转载请注明来自http://www.dlwjkj.com