[英]How to use HTML parser to get whats in a div tag or another tag in Java
I want to get text in a tag, ie 我想在标签中获取文本,即
<div id="title"> MotoGP </div>
I want to extract "MotoGP" from here. 我想从这里提取“ MotoGP”。 I'm using org.htmlparser
. 我正在使用org.htmlparser
。
I've tried 我试过了
NodeList nodes = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("div"),
new HasAttributeFilter("id", "title")));
SimpleNodeIterator nodeIterator = nodes.elements();
while (nodeIterator.hasMoreNodes()) {
HeadingTag tag = (HeadingTag)node;
System.out.println(tag.getStringText());
Looks like something like this: 看起来像这样:
Parser p;
// initialize p somehow
p = createParser(html /* actual html String */,
charset /* null for default */);
NodeList nl = p.extractAllNodesThatMatch(
new HasAttributeFilter("id", "title")); // or other id...
// if you want the text of the 1st matching node:
System.out.println(nl.elementAt(0).getText());
See especially: 特别看到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.