java - 我如何使用java中的html解析器从DIV中获取数据

标签 java html html-parsing

我正在使用Java html解析器(link text)来尝试解析这一行。

<td class=t01 align=right><div id="OBJ123" name=""></div></td>

但我正在寻找像我在网络浏览器上看到的那样的值,这是一个数字。你能帮我得到这个值吗?

如果您需要更多详细信息,请告诉我。
谢谢

最佳答案

从文档中,您所要做的就是找到所有 DIV id 也为 OBJ123 的元素并取第一个结果的值。

NodeList nl = parser.parse(null); // you can also filter here

NodeList divs = nl.extractAllNodesThatMatch(
  new AndFilter(new TagNameFilter("DIV"), 
    new HasAttributeFilter("id", "OBJ123")));

if( divs.size() > 0 ) {
  Tag div = divs.elementAt(0);
  String text = div.getText(); // this is the text of the div
}

更新:如果您正在查看ajax url ,您可以使用类似的代码,例如:

// make some sort of constants for all the positions
const int OPEN_PRICE = 0;
const int HIGH_PRICE = 1;
const int LOW_PRICE = 2;
// ....

NodeList nl = parser.parse(null); // you can also filter here

NodeList values = nl.extractAllNodesThatMatch(
  new AndFilter(new TagNameFilter("TD"), 
    new HasAttributeFilter("class", "t1")));

if( values.size() > 0 ) {
  Tag openPrice = values.elementAt(OPEN_PRICE);
  String openPriceValue = openPrice.getText(); // this is the text of the div
}

关于java - 我如何使用java中的html解析器从DIV中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527672/

相关文章:

javascript - 围绕表单垂直居中页面

Java - 带有键、keyTitle、值、valueTitle 的映射

java - 为什么 MySQL 数据库访问需要在服务器 URL 中使用正斜杠?

javascript - 使用 HTML5 JS 框架进行本地存储?

html - 在 Zend SubForm 中,如何将 Legend escape 设置为 false?

java - Jsoup Java HTML 解析器 : Executing Javascript events

python - beautifulsoup 和无效的 html 文档

c++ - 如何知道 Tar 解析器里面的文件

java - 在 Java 中使用正则表达式划分数字列表

java - Eclipse 中的奇怪行为