【问题标题】:Disable robots.txt check in nutch禁用 robots.txt 签入 nutch
【发布时间】:2013-02-15 14:40:23
【问题描述】:

我想禁用 robots.txt 检查 Nutch 并从网站抓取所有内容。 Disable 表示在获取或解析任何网站之前,跳过检查 robots.txt。 这可能吗?

【问题讨论】:

  • 真的不应该这样做。看起来该项目是作为 Apache 许可证,版本 2.0 下的源代码提供的,因此您可以编辑 RobotRulesRobotRulesParser 中的行以将每个 URL 视为“允许”。如果您在此处进行任何更改,您应该向用户代理添加一些内容以反映它不是软件的正常版本。
  • 我正在寻找一种方法来禁用使用配置而不是更改代码。
  • 在这个link 有一个类似问题的讨论。希望对您有所帮助。

标签: web-crawler nutch


【解决方案1】:

虽然这个问题很老了,但我个人觉得它仍然值得回答。

是的,可以禁用 robots.txt 流(但您需要更改和构建 Nutch 源代码)。

注意:nutch 没有提供任何特定的配置来在获取实际 URL 之前禁用 robots.txt 获取。因为您所说的听起来像是 URL/域滥用,并且您想要访问它,而不管网站试图通过 robots.txt 说明其资源的内容。

怎么可能? 如果您有一个真正需要跳过 robots.txt 的自定义用例,那么您可以执行以下操作

nutch 中的大部分插件(protocal-(http|httpclient|selenium|okhttp)) 使用 HttpRobotRulesParser 类来获取和解析 robots.txt 的内容

HttpRobotsRulesParser 这个特殊方法中,您可以解析并返回 rules 对象

public BaseRobotRules getRobotRulesSet(Protocol http, URL url,
      List<Content> robotsTxtContent) {

    if (LOG.isTraceEnabled() && isWhiteListed(url)) {
      LOG.trace("Ignoring robots.txt (host is whitelisted) for URL: {}", url);
    }

    String cacheKey = getCacheKey(url);
    BaseRobotRules robotRules = CACHE.get(cacheKey);

    if (robotRules != null) {
      return robotRules; // cached rule
    } else if (LOG.isTraceEnabled()) {
      LOG.trace("cache miss " + url);
    }

    boolean cacheRule = true;
    URL redir = null;

    if (isWhiteListed(url)) {
      // check in advance whether a host is whitelisted
      // (we do not need to fetch robots.txt)
      robotRules = EMPTY_RULES;
      LOG.info("Whitelisted host found for: {}", url);
      LOG.info("Ignoring robots.txt for all URLs from whitelisted host: {}",
          url.getHost());

    } else {
      try {
        URL robotsUrl = new URL(url, "/robots.txt");
        Response response = ((HttpBase) http).getResponse(robotsUrl,
            new CrawlDatum(), false);
        if (robotsTxtContent != null) {
          addRobotsContent(robotsTxtContent, robotsUrl, response);
        }
        // try one level of redirection ?
        if (response.getCode() == 301 || response.getCode() == 302) {
          String redirection = response.getHeader("Location");
          if (redirection == null) {
            // some versions of MS IIS are known to mangle this header
            redirection = response.getHeader("location");
          }
          if (redirection != null) {
            if (!redirection.startsWith("http")) {
              // RFC says it should be absolute, but apparently it isn't
              redir = new URL(url, redirection);
            } else {
              redir = new URL(redirection);
            }

            response = ((HttpBase) http).getResponse(redir, new CrawlDatum(), false);
            if (robotsTxtContent != null) {
              addRobotsContent(robotsTxtContent, redir, response);
            }
          }
        }

        if (response.getCode() == 200) // found rules: parse them
          robotRules = parseRules(url.toString(), response.getContent(),
              response.getHeader("Content-Type"), agentNames);

        else if ((response.getCode() == 403) && (!allowForbidden))
          robotRules = FORBID_ALL_RULES; // use forbid all
        else if (response.getCode() >= 500) {
          //cacheRule = false; // try again later to fetch robots.txt
          robotRules = EMPTY_RULES;
        } else
          robotRules = EMPTY_RULES; // use default rules
      } catch (Throwable t) {
        if (LOG.isInfoEnabled()) {
          LOG.info("Couldn't get robots.txt for " + url + ": " + t.toString());
        }
        //cacheRule = false; // try again later to fetch robots.txt
        robotRules = EMPTY_RULES;
      }
    }

    if (cacheRule) {
      CACHE.put(cacheKey, robotRules); // cache rules for host
      if (redir != null && !redir.getHost().equalsIgnoreCase(url.getHost())
          && "/robots.txt".equals(redir.getFile())) {
        // cache also for the redirected host
        // if the URL path is /robots.txt
        CACHE.put(getCacheKey(redir), robotRules);
      }
    }

    return robotRules;
  }

你可以继续用下面的方法替换

  @Override
  public BaseRobotRules getRobotRulesSet(Protocol http, URL url,
      List<Content> robotsTxtContent) {
       return EMPTY_RULES; // always return empty rules to skip robots.txt access.
    }

您只是在通过返回 EMPTY_RULES 来嘲笑行为。

提示:始终建议阅读和访问 robots.txt 中提到的资源。

【讨论】:

  • 在我的一个项目中,我也做了同样的事情,为此我定制了 Fetcher Reducer 类。它适用于我的情况。
【解决方案2】:

据我了解,我们无法在 nutch 中禁用 robots.txt。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多