如何使用HTML解析器在Java中获取div标记或其他标记中的内容
我想在标记中获取文本,即如何使用HTML解析器在Java中获取div标记或其他标记中的内容,java,html,html-parsing,Java,Html,Html Parsing,我想在标记中获取文本,即 <div id="title"> MotoGP </div> 看起来像这样: Parser p; // initialize p somehow p = createParser(html /* actual html String */, charset /* null for default */); NodeList nl = p.extractAllNodesThatMatch( new HasAttrib
<div id="title"> MotoGP </div>
看起来像这样:
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());
特别见:
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());