这并不简单。 我正在解析页面( 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ón Térmica。浏览器将突出显示包含该元素的代码。
将鼠标放在突出显示的代码上,右键单击它并选择复制唯一选择器
。
然后你可以使用这段代码来获取元素 -
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
循环来运行这些选择器,并获取您需要的所有信息。
关于java - Jsoup 从表内表获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678000/