【发布时间】:2021-05-11 18:48:23
【问题描述】:
我尝试了许多不同的 XPath 值,只是不明白为什么我无法使用 Apache Tika 检索我想要的内容。 我想检索随机网页上链接的 href 属性值。我设法找出如何提取标签内的内容,但试图获取属性值总是返回空。 我究竟做错了什么?! 下面是我的代码, 非常感谢
XPathParser xhtmlParser = new XPathParser ("xhtml", XHTMLContentHandler.XHTML);
Matcher anchorLinkContentMatcher = xhtmlParser.parse("//xhtml:a/@xhtml:href/text()");
ContentHandler handler = new MatchingContentHandler(
new ToHTMLContentHandler(), anchorLinkContentMatcher);
HtmlParser parser = new HtmlParser();
ParseContext pcontext = new ParseContext();
try {
parser.parse(urlContentStream, handler, new Metadata(),pcontext);
System.out.println(handler);
}
catch (Exception e)
{....}
我尝试过这些不同的 XPath:
//xhtml:a/@xhtml:href
//xhtml:a/@href/text()
//xhtml:a/@href
//@xhtml:href/text()
【问题讨论】:
标签: java xpath attributes apache-tika