首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在StormCrawler中处理重定向域

在StormCrawler中处理重定向域
EN

Stack Overflow用户
提问于 2021-02-09 07:30:12
回答 2查看 73关注 0票数 0

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-09 07:48:14

过滤器不区分从重定向找到的URL和来自页面中链接的URL。您可以简单地关闭基于深度的过滤器,而有一个自定义的解析过滤器,以便在必要时限制外部链接。

票数 1
EN

Stack Overflow用户

发布于 2021-02-14 11:12:47

我对MaxDepthFilter作了如下修改:

代码语言:javascript
复制
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;
        }
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66114673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档