[英]How to use HTML parser to get whats in a div tag or another tag in Java
我想在標簽中獲取文本,即
<div id="title"> MotoGP </div>
我想從這里提取“ MotoGP”。 我正在使用org.htmlparser
。
我試過了
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());
看起來像這樣:
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());
特別看到:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.