Jsoup 从 table 中获取数据 table

Jsoup Get data from table inside a table

这不简单。 我正在解析一个页面 (http://www.catedralaltapatagonia.com/invierno/partediario.php?default_tab=0) 我需要其他 table 中的 table 中的数据,但我无法访问,因为我总是收到有关 Invalid index Index

的错误

我需要这个值

这个单元格在 td 里面,在 tr 里面,在 table 里面,这个 table 在另一个 table 里面。 每列单元格都在 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ón Térmica。浏览器将突出显示包含该元素的代码。
将鼠标放在突出显示的代码上,右键单击它并 select 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();

现在,对你需要的所有元素都这样做,你会发现一个模式——有三个表(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ón Té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)

Viento 是

#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 循环对那些 selector 进行处理,并获取您需要的所有信息。