0

使用 Storm Crawler 1.13 和 Elastic Search 6.5.2。在 TextExtractor 中工作。我同样排除了脚本样式标签,我想删除标题标签。我正在应用以下配置,但它不适用于所有结果。我想保留h1h2h3只删除标题命名标签。有什么建议么。

网页

<header id="section-header" class="section section-header">
</header>

<h1 class="title" id="page-title">Good Morning..</h1>

爬虫配置文件

  textextractor.include.pattern:
   - DIV[id="maincontent"]
   - DIV[itemprop="articleBody"]
   - ARTICLE

  textextractor.exclude.tags:
   - STYLE
   - SCRIPT
   - HEADER
   - FOOTER
4

1 回答 1

2

我无法在我的本地机器上重现您的问题。这可能是您这边的配置缺陷,或者您所指的网站很特殊。

您是否确认您的自定义crawler-conf.yaml已正确加载并且textextractor.exclude.tags包含在加载的配置中?

我做了以下步骤试图重现你的问题:

  1. 我查看了1.13StormCrawler 的发布源。
  2. 我将以下单元测试添加到TextExtractorTest.java
    @Test
    public void testRemoveHeaderElements() throws IOException {
        Config conf = new Config();
        HashSet<String> excluded = new HashSet<>();
        excluded.add("HEADER");
        excluded.add("FOOTER");
        excluded.add("SCRIPT");
        excluded.add("STYLE");
        conf.put(TextExtractor.EXCLUDE_PARAM_NAME, PersistentVector.create(excluded));

    HashSet&lt;String&gt; included = new HashSet&lt;&gt;();
    included.add("DIV[id=\"maincontent\"]");
    included.add("DIV[itemprop=\"articleBody\"]");
    included.add("ARTICLE");
    conf.put(TextExtractor.INCLUDE_PARAM_NAME, PersistentVector.create(included));

    TextExtractor extractor = new TextExtractor(conf);

    String content = "&lt;header id=\"section-header\" class=\"section section-header\"&gt;&lt;/header&gt;&lt;h1 class=\"title\" id=\"page-title\"&gt;Good Morning..&lt;/h1&gt;";

    Document jsoupDoc = Parser.htmlParser().parseInput(content,
            "http://stormcrawler.net");
    String text = extractor.text(jsoupDoc.body());

    assertEquals("Good Morning..", text);
}

这个组件的单元测试TextExtractor通过了。接下来,我确实将具有以下 HTML 代码的网站上传到本地部署的 Web 服务器:

<header id="section-header" class="section section-header">
</header>



Good Morning..


提取的文本内容为:Good Morning..,根据您的要求应该可以。

于 2019-01-16T13:26:03.457 回答