我正在使用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/