Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用html解析器从DIV中获取数据_Java_Html_Html Parsing - Fatal编程技术网

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

如何在java中使用html解析器从DIV中获取数据,java,html,html-parsing,Java,Html,Html Parsing,我正在使用Java html parser()来解析这一行 <td class=t01 align=right><div id="OBJ123" name=""></div></td> 但我正在寻找我在网络浏览器上看到的价值,这是一个数字。你能帮我得到这个值吗 如果您需要更多详细信息,请告诉我。 感谢文档中的,您所要做的就是查找id为OBJ123的所有DIV元素,并获取第一个结果的值 NodeList nl = parser.parse(nu

我正在使用Java html parser()来解析这一行

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

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

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

感谢文档中的,您所要做的就是查找id为
OBJ123
的所有
DIV
元素,并获取第一个结果的值

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
}
更新:如果您正在查看,可以使用类似的代码,如:

// 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
}

这部分html不会在浏览器中显示任何文本,除非有javascript对其进行操作。如果您指的是其他值,请向我们指出您感兴趣的值。例如,我想获取浏览器显示的此链接的值。如何获取。是的,必须是java脚本。我希望链接源代码能帮助您更好地理解这个问题。如果您试图删除此网站,则在页面加载时div将不包含值,并且仅在javascript执行时才会填充。除非您的解析器可以执行javascript,否则您将不得不以其他方式对其进行刮取,或许可以查看javascript并将其刮取。是否有指向任何工具的指针来查找java脚本并将其刮取,或者指向可以执行java脚本的解析器?任何指向与之相关的文档的指针都将不胜感激。您真正想做的是刮取此url:并找到所有
TD
标记,使用
class=t1
,并对其进行索引,以便第一个是开盘价,第二个是高价,等等。。你将不得不摆弄这些位置。