微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用 Apache Tika 和 XPath 获取属性值

如何解决尝试使用 Apache Tika 和 XPath 获取属性值

我尝试了许多不同的 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()

解决方法

你就快到了……你需要:

//xhtml:a/@href

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。