java - 从抓取中删除 header

标签 java web-crawler stormcrawler

正在开发 Storm Crawler 1.13 和 Elastic Search 6.5.2。在文本提取器中工作。我排除 scriptstyle 标签,类似地我想删除 header 标签。我正在应用以下配置,但它并不适用于所有结果。我想保留 h1h2h3 仅删除 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 已包含在加载的配置中?

我执行了以下步骤尝试重现您的问题:

  1. 我查看了 StormCrawler 的 1.13 版本源。
  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));

<pre><code>    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);
}
</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/

相关文章:

java - 将广播应用程序转变为服务

java - 在 Android 和 PC 之间创建 Wifi 点对点连接

java - 在 Java 上运行网络蜘蛛

elasticsearch - Stormcrawler -> Elasticsearch 的最佳设置,如果爬行的礼貌不是问题?

java - 使用 StormCrawler 抓取某些 url 时出现 X509 证书异常

java - activity_main 无法解析或不是字段

python - 从多个网站提取文本

python - 用scrapy爬表,网站有不正常的html代码。

elasticsearch - 如何使用 StormCrawler 将网站内容存储在状态索引中?

java - 当我尝试插入数据时,executeQuery() 不起作用?