爬虫 (反爬)

再爬虫过程中,我们爬着爬着,他就会阻止你浏览页面,说明对方已经在页面上设置了反爬。

而我,今天用 装饰器的方法,也制定了一个反爬!

下面 ,则是详解代码

def limit(seconds=1):
# 定义内部方法
def rate_limit(func):
def func_limit(request):
# 设置当前时间
now =time.time()
# 获取首次来访时间
request_time = request.session.get('req_time',0)
# 做减法
in_time = int(now) - request_time

# 判断访问者在一秒内来了不止一次
if in_time < seconds:
# 抛出异常
return HttpResponse('你是爬虫,不要来了',status=403)
else:
# 来的时间点存储
request.session['req_time']=time.time()
# 让访问者继续访问
ret = func(request)
return ret
return func_limit
return rate_limit
posted @ 2019-01-20 16:19  男神鹏●詹姆斯  阅读(151)  评论(0编辑  收藏  举报