html - 避免使用 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 应用模板”才能达到目标。
提前谢谢你们!
解决方案
使用 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;
}}
推荐阅读
- angular - 自定义 ag gird 中的最后一行
- key-value - 如何将 consul 与 cloudflare 连接?
- reactjs - 如何在 Electron 和 CRA 的生产环境中使用 loadUrl 或 loadFile?
- scala - `Coder[P <: Product]`的Scala 2宏类型类派生以错误`P不带参数`结束
- discord.py - 如何使用 discord.py 获取主(默认)文本通道?
- mongodb - 如何在猫鼬中实现每个服务和私有表的模式
- javascript - 如何拆分 JavaScript 多维数组并获取最后一个数组?
- flutter - 如何防止 iphone 缺口与书签桌面应用程序中的内容重叠?
- r - 如何计算r中两年的移动平均值
- google-chrome-extension - 仅在一台主机上启用 Chrome 扩展操作按钮