我无法在我的本地机器上重现您的问题。这可能是您这边的配置缺陷,或者您所指的网站很特殊。
您是否确认您的自定义crawler-conf.yaml
已正确加载并且textextractor.exclude.tags
包含在加载的配置中?
我做了以下步骤试图重现你的问题:
- 我查看了
1.13
StormCrawler 的发布源。
- 我将以下单元测试添加到
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<String> included = new HashSet<>();
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 = "<header id=\"section-header\" class=\"section section-header\"></header><h1 class=\"title\" id=\"page-title\">Good Morning..</h1>";
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..
,根据您的要求应该可以。