处理StormCrawler中的重定向域



我正在做一个基于StormCrawler的项目。我们的需求之一是找到重定向到另一个域的域。在StormCrawler中,每个重定向的URL都被认为是爬行的一个深度。例如,对于具有两个重定向步骤的域,我们需要以depth=2进行爬行。如何在不考虑深度的情况下解析所有重定向域在履带车里?

过滤器不区分从重定向中找到的url和来自页面中的链接的url。您可以简单地停用基于深度的过滤器,并在必要时使用自定义解析过滤器来限制外联。

我已经修改了MaxDepthFilter如下:

public class MaxDepthFilter implements URLFilter {
private static final Logger LOG = LoggerFactory
.getLogger(MaxDepthFilter.class);
private int maxDepth;

@Override
public void configure(Map stormConf, JsonNode paramNode) {
JsonNode node = paramNode.get("maxDepth");
if (node != null && node.isInt()) {
maxDepth = node.intValue();
} else {
maxDepth = -1;
LOG.warn("maxDepth parameter not found");
}

}
@Override
public String filter(URL pageUrl, Metadata sourceMetadata, String url) {
int depth = getDepth(sourceMetadata, MetadataTransfer.depthKeyName);

boolean containsRedir = containsRedirect(sourceMetadata, "_redirTo");

// is there a custom value set for this particular URL?
int customMax = getDepth(sourceMetadata,
MetadataTransfer.maxDepthKeyName);
if (customMax >= 0) {
return filter(depth, customMax, url);
}
// rely on the default max otherwise
else if (maxDepth >= 0) {
if(containsRedir)
return url;
else
return filter(depth, maxDepth, url);
}
return url;
}
private String filter(int depth, int max, String url) {
// deactivate the outlink no matter what the depth is
if (max == 0) {
return null;
}
if (depth >= max) {
return null;
}
return url;
}

private int getDepth(Metadata sourceMetadata, String key) {
if (sourceMetadata == null) {
return -1;
}
String depth = sourceMetadata.getFirstValue(key);
if (StringUtils.isNumeric(depth)) {
return Integer.parseInt(depth);
} else {
return -1;
}
}

private boolean containsRedirect(Metadata sourceMetadata, String key) {
if (sourceMetadata == null) {
return false;
}
String redir = sourceMetadata.getFirstValue(key);
if (StringUtils.isNotBlank(redir)) {
return true;
} else {
return false;
}
}
}

它是否正常工作或陷入无限循环?

最新更新