zoukankan      html  css  js  c++  java
  • 爬虫 (反爬)

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

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

    下面 ,则是详解代码

    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
  • 相关阅读:
    支付方法及注意事项
    网站负载均衡策略
    工作成长
    java内存机制
    关于前途的一些思考
    git记录
    关于博客
    如何为公司创造价值?
    遍历集合方法总结
    二叉树和红黑二叉树
  • 原文地址:https://www.cnblogs.com/lyp0626/p/10295266.html
Copyright © 2011-2022 走看看