【问题标题】:How can I prevent Solr from adding headers and footers?如何防止 Solr 添加页眉和页脚?
【发布时间】:2013-04-16 11:37:25
【问题描述】:

我有一个抓取网站内容的网络爬虫 (Ncrawler),并且我添加了代码以将数据索引到 solr。我的要求是避免将网站的页眉、页脚和导航窗格添加到 solr 进行索引。

有没有办法做到这一点?任何帮助将不胜感激。

谢谢, 阿努

【问题讨论】:

    标签: c#-4.0 solr web-crawler


    【解决方案1】:

    您可以利用 HtmlDocumentProcessor 在构造函数上具有 filterTextRules 参数的类。此参数需要作为 Dictionary<string,string> 传递,并带有用于过滤标记的开始和结束字符串。

    例如,假设您的 html 页面中有页眉和页脚,它们在 html 中的结构如下:

     <!-- Begin Header -->
     all header markup is here
     <!-- End Header -->
    
     <!-- Begin Footer -->
     all footer markup is here
     <!-- End Footer -->
    

    在这种情况下,您可以按如下方式在管道中初始化 HtmlDocumentProcessor:

        var pipelines = new IPipelineStep[]
                   {
                      new HtmlDocumentProcessor(
                            new Dictionary<string, string>
                                {
                                   {"<!--Begin Header", "<!--End Header"},
                                   {"<!--Begin Footer", "<!--End Footer"},
                                }, 
                                null), 
                             new PdfIFilterProcessor(), 
                             new TextDocumentProcessor(), 
                    };
    
        using (var crawler = new NCrawler.Crawler(new Uri("http://ncrawler.codeplex.com"),
                 pipelines))
        {
              //Processing here
        }
    

    希望这会有所帮助。有关 filterTextRules 参数及其工作原理的更多详细信息,请参阅HtmlDocumentProcessor source

    【讨论】:

    • 谢谢你,非常感谢...... :) @Paige Cook。你的回答真的对我很有帮助......不仅在这个问题上,而且在最后一个问题上。是否有任何关于 ncrawler-solr 集成的参考链接或电子书值得一读?
    • 很高兴这些对您有所帮助。不幸的是,没有任何 NCrawler-Solr 集成参考,我通过反复试验了解到了所有这些。
    猜你喜欢
    • 2011-11-27
    • 2010-12-03
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    相关资源
    最近更新 更多