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

How to prevent rollbar from reporting SEO crawlers activities?

我在 rails 应用程序中设置了滚动条。它不断报告 recordnotfound,这是搜索引擎优化爬虫(即 Google 机器人、百度、findxbot 等)搜索已删除 post.

的结果

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

看起来你正在使用 rollbar-gem,所以你想使用 Rollbar::Ignore 来告诉 Rollbar 忽略由 spider

引起的错误
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 = 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.

TL;DR:

# ./initializers/rollbar.rb
#
# 
# 
# 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

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

小心魔法注释 frozen_string_literal 如果你的 Ruby 版本低于 2.3,请使用 =~ 而不是 match?

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

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

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

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

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

=======

EDIT1:如果你需要防止 rollbar 在 js 上通知,检查 https://github.com/ZLevine/rollbar-ignore-crawler-errors 仓库是个好主意。