【问题标题】:How to prevent rollbar from reporting SEO crawlers activities?如何防止 rollbar 报告 SEO 爬虫活动?
【发布时间】:2016-04-13 04:14:22
【问题描述】:

我在 Rails 应用程序中设置了滚动条。它不断报告记录未找到,这是由于 SEO 爬虫(即 Google bot、百度、findxbot 等)搜索已删除帖子的结果。

如何防止 rollbar 报告 SEO 爬虫活动。

【问题讨论】:

    标签: ruby-on-rails rollbar


    【解决方案1】:

    TL;DR:

    # ./initializers/rollbar.rb
    #
    # https://stackoverflow.com/questions/36588449/how-to-prevent-rollbar-from-reporting-seo-crawlers-activities
    # 
    # frozen_string_literal: true
    
    crawlers = %w[Facebot Twitterbot YandexBot bingbot AhrefsBot crawler MJ12bot Yahoo GoogleBot Mail.RU_Bot SemrushBot YandexMobileBot DotBot AppleMail SeznamBot Baiduspider]
    regexp = Regexp.new(Regexp.union(*crawlers).source, Regexp::IGNORECASE)
    
    Rollbar.configure do |config|
      ignore_bots = lambda do |options|
        agent = options.fetch(:scope).fetch(:request).call.fetch(:headers)['User-Agent']
        raise Rollbar::Ignore if agent.match?(regexp)
      end
    
      config.before_process << ignore_bots
    
      ...
    end
    

    =======================

    如果您的 Ruby 版本低于 2.3,请注意魔术注释 frozen_string_literal 并使用 =~ 而不是 match?

    这里我使用了一个将被转换为正则表达式的数组。我这样做是因为我想防止将来开发人员的语法和转义相关错误,并出于同样的原因添加忽略大小写的东西。

    所以在正则表达式中你会看到一个Mail\.RU_Bot,而不是任何错误。

    此外,在您的情况下,您可以使用简单的词 bot 而不是许多爬虫,但要小心不寻常的用户代理。就我而言,我想知道我网站上的所有爬虫,所以我想出了这个解决方案。工作部分的另一个示例:我的生产站点上有crawlercrawler4j。我只在数组中使用crawler 来防止通知他们两个。

    我想说的最后一件事——我的解决方案不是非常理想,但它确实有效。我希望有人能分享我的代码的优化版本。这也是我建议异步发送数据的主要原因,即使用 sidekiq、delayed_job 或任何你想要的,不要忘记查看相关的 wiki。

    我的回答是基于@AndrewSouthpaw 的解决方案 (?),这对我不起作用。希望获得批准的 wiki-copy-pasted @Jesse Gibbs 能够以某种方式进行审核。

    =======

    EDIT1:如果您需要防止滚动条通知 js,最好检查 https://github.com/ZLevine/rollbar-ignore-crawler-errors 存储库。

    【讨论】:

      【解决方案2】:

      看起来您正在使用rollbar-gem,因此您想使用Rollbar::Ignore 告诉Rollbar 忽略由蜘蛛引起的错误

      handler = proc do |options|
        raise Rollbar::Ignore if is_crawler_error(options)
      end
      
      Rollbar.configure do |config|
          config.before_process << handler
      end
      

      is_crawler_error 检测导致错误的请求是否来自爬虫。

      如果您使用rollbar.js 来检测客户端Javascript 中的错误,那么您可以使用checkIgnore 选项过滤掉由机器人引起的客户端错误:

      _rollbarConfig = {
        // current config...
        checkIgnore: function(isUncaught, args, payload) {
           if (window.navigator.userAgent && window.navigator.userAgent.indexOf('Baiduspider') !== -1) {
             // ignore baidu spider
             return true;
           }
           // no other ignores
           return false;
         }
      }
      

      【讨论】:

      • 在哪里可以获得有关 is_crawler_error 方法的更多详细信息?
      • 嗨,Alif - 抱歉不清楚这一点 - 您应该自己定义 is_crawler_error(或任何查找蜘蛛的方法)。它应该根据您要阻止的已知蜘蛛列表检查请求中的用户代理值。
      • 来到这里寻找有关如何实现处理程序以忽略某些请求的秘诀。如果它是来自文档的完整复制粘贴,不确定如何成为可接受的答案。
      【解决方案3】:

      这就是我所做的:

      is_crawler_error = Proc.new do |options|
        return true if options[:scope][:request]['From'] == 'bingbot(at)microsoft.com'
        return true if options[:scope][:request]['From'] == 'googlebot(at)googlebot.com'
        return true if options[:scope][:request]['User-Agent'] =~ /Facebot Twitterbot/
      end
      
      handler = proc do |options|
        raise Rollbar::Ignore if is_crawler_error.call(options)
      end
      
      config.before_process << handler
      

      基于these docs

      【讨论】:

        猜你喜欢
        • 2011-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多