正在开发 Storm Crawler 1.13 和 Elastic Search 6.5.2。在文本提取器中工作。我排除 script 和 style 标签,类似地我想删除 header 标签。我正在应用以下配置,但它并不适用于所有结果。我想保留 h1 、 h2 、 h3 仅删除 header 命名标签。有什么建议。
网页:
<header id="section-header" class="section section-header">
</header>
<h1 class="title" id="page-title">Good Morning..</h1>
crawlerconf.yaml
textextractor.include.pattern:
- DIV[id="maincontent"]
- DIV[itemprop="articleBody"]
- ARTICLE
textextractor.exclude.tags:
- STYLE
- SCRIPT
- HEADER
- FOOTER
最佳答案
我无法在本地计算机上重现您的问题。这可能是您这边的配置缺陷或者您引用的网站很特殊。
您是否确认您的自定义 crawler-conf.yaml
已正确加载,并且 textextractor.exclude.tags
已包含在加载的配置中?
我执行了以下步骤尝试重现您的问题:
- 我查看了 StormCrawler 的
1.13
版本源。 - 我将以下单元测试添加到
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));
<pre><code> 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);
}
</code></pre>
对 TextExtractor
组件的单元测试通过。接下来,我将包含以下 HTML 代码的网站上传到本地部署的 Web 服务器:
<header id="section-header" class="section section-header">
</header>
Good Morning..
提取出来的文字内容是:Good Morning..
,按照你的要求应该没问题。
关于java - 从抓取中删除 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150845/