是否担心高频率爬虫导致网站瘫痪?
别担心,现在有一个python写的神器——crawlerdetect,帮助你检测爬虫,保障网站的正常运转。
1.准备
开始之前,你要确保python和pip已经成功安装在电脑上噢,如果没有,请访问这篇文章:超详细python安装指南 进行安装。如果你用python的目的是数据分析,可以直接安装anaconda:python数据分析与挖掘好帮手—anaconda
windows环境下打开cmd(开始—运行—cmd),苹果系统环境下请打开terminal(command+空格输入terminal),准备开始输入命令安装依赖。
当然,我更推荐大家用vscode编辑器,把本文代码copy下来,在编辑器下方的终端运行命令安装依赖模块,多舒服的一件事啊:python 编程的最好搭档—vscode 详细指南。
在终端输入以下命令安装我们所需要的依赖模块:
pip install crawlerdetect看到 successfully installed xxx 则说明安装成功。
2.使用方法它可以通过user-agent、headers等请求头识别爬虫或机器人。
因此,你可以传递两种参数。第一种,使用user-agent检测机器人:
from crawlerdetect import crawlerdetectcrawler_detect = crawlerdetect(user_agent='mozilla/5.0 (iphone; cpu iphone os 7_1 like mac os x) applewebkit (khtml, like gecko) mobile (compatible; yahoo ad monitoring; https://help.yahoo.com/kb/yahoo-ad-monitoring-sln24857.html)')crawler_detect.iscrawler()# 如果是机器人,这条语句返回true第二种识别方式会用上全部headers参数,这种方式比单纯用user-agent精准,因为它判断的依据更加全面。
from crawlerdetect import crawlerdetectcrawler_detect = crawlerdetect(headers={'document_root': '/home/test/public_html', 'gateway_interface': 'cgi/1.1', 'http_accept': '*/*', 'http_accept_encoding': 'gzip, deflate', 'http_cache_control': 'no-cache', 'http_connection': 'keep-alive', 'http_from': 'googlebot(at)googlebot.com', 'http_host': 'www.test.com', 'http_pragma': 'no-cache', 'http_user_agent': 'mozilla/5.0 (macintosh; intel mac os x 10_8_4) applewebkit/537.36 (khtml, like gecko) chrome/28.0.1500.71 safari/537.36', 'path': '/bin:/usr/bin', 'query_string': 'order=closingdate', 'redirect_status': '200', 'remote_addr': '127.0.0.1', 'remote_port': '3360', 'request_method': 'get', 'request_uri': '/?test=testing', 'script_filename': '/home/test/public_html/index.php', 'script_name': '/index.php', 'server_addr': '127.0.0.1', 'server_admin': 'webmaster@test.com', 'server_name': 'www.test.com', 'server_port': '80', 'server_protocol': 'http/1.1', 'server_signature': '', 'server_software': 'apache', 'unique_id': 'vx6menrxerbusdeqgflaaaaas', 'php_self': '/index.php', 'request_time_float': 1461619728.0705, 'request_time': 1461619728})crawler_detect.iscrawler()# 如果是机器人,这条语句返回true你还可以识别相应爬虫的名字(如果有的话),通过这种方式,你能给一些著名的爬虫(如baiduspider、googlebot)添加白名单,不进行拦截。
from crawlerdetect import crawlerdetectcrawler_detect = crawlerdetect()crawler_detect.iscrawler('mozilla/5.0 (compatible; sosospider/2.0; +http://help.soso.com/webspider.htm)')# 如果是机器人,这条语句返回truecrawler_detect.getmatches()# sosospider有了这个工具,我们就可以实现实时的爬虫封禁:
**1. **实时监控网站上的http请求,检测其对应的headers.
**2. **如果识别到该请求是机器人(爬虫)发出的,就可将其ip记录下来。
**3. ** 将ip加入到nginx或apache的动态黑名单中,实现实时的爬虫封禁。
这一套流程我还没有试验过,大家有兴趣可以试试,理论上可行。
LED虚拟拍摄中LED屏幕的作用
交换机芯片背后的黑科技解密
智能手环新体验,让运动成为每天的乐趣
Littelfuse推出一个表面安装式保险丝系列产品
网站基本建设基本上步骤
crawlerdetect:Python 三行代码检测爬虫
基于NFC技术应用的茶叶防伪解决方案
回顾2020年电视面板市场的发展势态
未来能否实现是屏幕就能上网
空气在线检测系统在环境监测中的应用
浅析Antenna Switch Tuner测试方法
小间距LED优势与发展方向
电压逆变器IC形成高效轨道分配器
ADM-PCIE-8K5 PCIe加速器板卡的特性
电源ATE测试助力提高短路保护测试效率,提供软硬件解决方案
电子产业的商业重构技术与产品
如何使用Raspbian优化树莓派的功能
爱特梅尔推出灵活的低功耗单键电容式触摸控制器
如何在PCB上使用ISP插头和AVR编程器进行编程
基于DVB-S2标准技术的卫星数字机顶盒设计方案