首页 > 解决方案 > 避免使用 html-xsl 解析抓取 html 部分

问题描述

我正在使用 Watson Explorer FC 11.0.2,并且试图避免来自 Watson 爬虫的一些 html 标记。当时我正在使用 xslt 解析器从具有以下路径的 html 页面中提取元数据、标题和正文:

"/html/body/div[@class='page-wrapper']/div[@id='main']/ul[@class='sidebar grid-25']"

我使用的解析器如下:

<xsl:template match="/">
<document>

<xsl:apply-templates match="h2[@class='entry-title']" />

<xsl:for-each select="html/head/meta">

<xsl:if test="@name != '' and @content != 'null'">
<content>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="@content" />
</content>
</xsl:if>

</xsl:for-each>

<xsl:apply-templates match="div[@class='entry-content']" />

</document>

<xsl:apply-templates match="ul[@class='sidebar grid-25']" />


</xsl:template>

<xsl:template match="h2[@class='entry-title']">
<content name="title">
<xsl:value-of select="." />
</content>
</xsl:template>

<xsl:template match="div[@class='entry-content']">
<content name="snippet" weight="1" output-action="summarize" type="html">
<xsl:value-of select="." />
</content>
</xsl:template>


<xsl:template match="ul[@class='sidebar grid-25']">
<xsl:value-of select="." />
</xsl:template>

那么,我该如何处理这个问题呢?我真的不知道我必须在解析器中的哪里插入“xsl 应用模板”才能达到目标。

提前谢谢你们!

标签: htmlxmlxsltibm-watsonwatson-explorer

解决方案


使用 XSLT 从正文中清除 HTML 标记的一种方法是:使用org.w3c中的Tidy

例如

<xsl:template match="/">
    <document>
        <xsl:apply-templates match="h2[@class='entry-title']" />

        <xsl:for-each select="html/head/meta">
            <xsl:if test="@name != '' and @content != 'null'">
                <content>
                    <xsl:attribute name="name">
                        <xsl:value-of select="@name" />
                    </xsl:attribute>
                    <xsl:value-of select="@content" />
                </content>
            </xsl:if>
        </xsl:for-each>

        <xsl:apply-templates match="div[@class='entry-content']" />
    </document>

    <xsl:apply-templates select="ul[@class='sidebar grid-25']" />
</xsl:template>

<xsl:template match="h2[@class='entry-title']">
    <content name="title">
        <xsl:value-of select="htmlparser:parseHTMLtoDocument(.)" />
    </content>
</xsl:template>

<xsl:template match="div[@class='entry-content']">
    <content name="snippet" weight="1" output-action="summarize" type="html">
        <xsl:value-of select="htmlparser:parseHTMLtoDocument(.)" />
    </content>
</xsl:template>

<xsl:template match="ul[@class='sidebar grid-25']">
    <xsl:value-of select="htmlparser:parseHTMLtoDocument(.)" />
</xsl:template>

您可以创建一个名为com.xyz.commons.xsl.HtmlDocumentParser的类,如下所示并调用它的方法:

public class HtmlDocumentParser {
private static Logger log = Logger.getLogger(HtmlDocumentParser.class);
private static Log4jPrintWriter log4j = new Log4jPrintWriter(log, Level.WARN);

public static Document parseHTMLtoDocument(final String input) {
    return parseHTMLtoDocument(input, "UTF-8");
}

public static Document parseHTMLtoDocument(final String input, final String encoding) {

    final String htmlInput = String
            .format("<!DOCTYPE HTML><html>\n<head>\n<title>\n</title>\n</head>\n<body>\n%s</body></html>", input);
    Tidy tidy = new Tidy();
    tidy.setInputEncoding(encoding);
    tidy.setOutputEncoding(encoding);
    tidy.setXHTML(true);
    tidy.setXmlOut(true);
    tidy.setEncloseBlockText(true);
    tidy.setEncloseText(true);
    tidy.setMakeBare(true);
    tidy.setMakeClean(true);
    tidy.setWord2000(true);
    tidy.setDropFontTags(true);
    tidy.setQuiet(true);
    tidy.setErrout(log4j);

    Document doc = tidy.parseDOM(new ByteArrayInputStream(htmlInput.getBytes(Charset.forName(encoding))), null);
    return doc;
}}

推荐阅读