4

我需要爬取一个站点以进行一些检查,以了解这些 URL 是否定期可用。为此,我使用 crawler4j。

我的问题来自一些禁用了机器人的<meta name="robots" content="noindex,nofollow" />网页,由于它拥有的内容,因此在搜索引擎中不索引这些网页是有意义的。

尽管禁用了 RobotServer 的配置,crawler4j 也没有关注这些链接。这必须很容易robotstxtConfig.setEnabled(false);

RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
robotstxtConfig.setUserAgentName(USER_AGENT_NAME);
robotstxtConfig.setEnabled(false);
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
WebCrawlerController controller = new WebCrawlerController(config, pageFetcher, robotstxtServer);
...

但是所描述的网页仍然没有被探索。我已经阅读了代码,这必须足以禁用机器人指令,但它没有按预期工作。也许我跳过了什么?我已经用版本3.53.6-SNAPSHOT相同的结果对其进行了测试。

4

2 回答 2

2

我正在使用新版本

   <dependency>
        <groupId>edu.uci.ics</groupId>
        <artifactId>crawler4j</artifactId>
        <version>4.1</version>
    </dependency>`

像这样设置 RobotstxtConfig 后,它正在工作:

    RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
    robotstxtConfig.setEnabled(false);

Crawler4J 中的测试结果和源代码证明:

public boolean allows(WebURL webURL) {
if (config.isEnabled()) {
  try {
    URL url = new URL(webURL.getURL());
    String host = getHost(url);
    String path = url.getPath();

    HostDirectives directives = host2directivesCache.get(host);

    if ((directives != null) && directives.needsRefetch()) {
      synchronized (host2directivesCache) {
        host2directivesCache.remove(host);
        directives = null;
      }
    }

    if (directives == null) {
      directives = fetchDirectives(url);
    }

    return directives.allows(path);
  } catch (MalformedURLException e) {
    logger.error("Bad URL in Robots.txt: " + webURL.getURL(), e);
  }
}

return true;
}

当 Enabled 设置为 false 时,它​​将不再进行检查。

于 2015-06-24T01:20:01.650 回答
0

为什么不在 crawler4j 中排除有关 Robotstxt 的所有内容?我需要抓取一个站点并忽略机器人,这对我有用。

我像这样在 .crawler 中更改了 CrawlController 和 WebCrawler :

WebCrawler.java:

删除

private RobotstxtServer robotstxtServer;

删除

this.robotstxtServer = crawlController.getRobotstxtServer();

编辑

 if ((shouldVisit(webURL)) && (this.robotstxtServer.allows(webURL)))
 -->
 if ((shouldVisit(webURL)))

编辑

if (((maxCrawlDepth == -1) || (curURL.getDepth() < maxCrawlDepth)) && 
              (shouldVisit(webURL)) && (this.robotstxtServer.allows(webURL)))
-->
if (((maxCrawlDepth == -1) || (curURL.getDepth() < maxCrawlDepth)) && 
              (shouldVisit(webURL)))

CrawlController.java:

删除

import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;

删除

 protected RobotstxtServer robotstxtServer;

编辑

public CrawlController(CrawlConfig config, PageFetcher pageFetcher, RobotstxtServer robotstxtServer) throws Exception
-->
public CrawlController(CrawlConfig config, PageFetcher pageFetcher) throws Exception

删除

this.robotstxtServer = robotstxtServer;

编辑

if (!this.robotstxtServer.allows(webUrl)) 
{
  logger.info("Robots.txt does not allow this seed: " + pageUrl);
} 
else 
{
  this.frontier.schedule(webUrl);
}
-->
this.frontier.schedule(webUrl);

删除

public RobotstxtServer getRobotstxtServer()
{
  return this.robotstxtServer;
}
public void setRobotstxtServer(RobotstxtServer robotstxtServer)
{
  this.robotstxtServer = robotstxtServer;
}

希望这就是你要找的。

于 2014-08-27T12:14:48.393 回答