簡體   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