繁体   English   中英

Jsoup从表内的表获取数据

[英]Jsoup Get data from table inside a table

这并不简单。 我正在解析页面( http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0 )我需要其他表中的表中包含的数据,但是我无法访问,因为我收到有关无效索引的所有错误指数

我需要这个值

我需要的细胞

此单元格位于tr内的td内,表内,而此表位于另一个表内。 单元格的每一列都在一个div id“ meteo_info”内,并且在每个td内都有一个相同的名称div id。

我尝试这种方式没有成功

      Elements base1=document.select("div#pd_foto_fondo");
            Elements base2 = base1.select("table");
            Elements base3 = base2.select("tr");
            Elements base4 = base3.select("table");
            Elements base5 = base4.select("tr");
            Elements base6 = base5.select("td");
            Element base7 =base6.get(0);
            Element div1 = base7.getElementById("meteo_info");
            Elements tables1 = div1.getElementsByTag("table");
            Element table1 = tables1.get(0);

            String text2 = table1.getElementsByTag("tr").get(3).getElementsByTag("td").get(2).text();

我在Asyntask doInBackground内部使用此代码

首先,在您的应用中下载网页时,更改“ USER AGENT字段以匹配您在计算机上使用的浏览器。 我将确保您使用相同的标签在应用程序中获得完全相同的页面。
我使用FF,但如果使用其他浏览器,则应该几乎相同-
打开开发人员工具(在FF中为F12),选择检查器,然后选择元素选择器(FF-最左侧的工具)。 之后,选择您想要获得的元素之一,比方说SECTOR BASE的SensaciónTérmica。 浏览器将突出显示包含该元素的代码。
将鼠标置于高亮代码上方,右键单击它,然后选择Copy unique selector
然后,您可以使用此代码获取元素-

Elements e = doc.select("#pd_foto_fondo > table:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(3)"); 

您可以通过以下方式获得价值

e.text();

现在,针对所需的所有元素进行操作,您将找到一个模式-有3个表(SECTOR BASE,SECTOR INTERMEDIO,SECTOR SUPERIOR),并且它们的ID位于末尾的第7位(不容易看到,行太长...)-

#pd_foto_fondo > table:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(3)
#pd_foto_fondo > table:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(3)
#pd_foto_fondo > table:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(3)

而且,每一行都有不同的ID,这次是末尾的第二行。 SensaciónTérmica是

#pd_foto_fondo > table:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(4) > td:nth-child(3)

而维恩托是

#pd_foto_fondo > table:nth-child(5) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > div:nth-child(1) > div:nth-child(3) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(3)

(请注意最后两行的4和5)。
您可以使用两个嵌套的for循环运行这些选择器,并获取所需的所有信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM