[英]Trouble Getting information from html tables in java
我想從該站點內的第一張表中獲取信息鏈接
這是我的代碼
Document document = Jsoup.parse(DownloadPage("http://www.transtejo.pt/clientes/horarios" +
"-ligacoes-fluviais/ligacao-barreiro-terreiro-do-paco/#dias-uteis"));
Elements table = document.select("table.easy-table-creator:nth-child(1) tbody");
Elements trAll = table.select("tr");
//For the Table Hour
Elements tr_first = table.select("tr:nth-child(1)");
Element tr = tr_first.get(1);
Elements td = tr.getElementsByTag("td");
for(int i = 0; i < td.size(); i++) {
Log.d("TIME TABLE:"," " + td.get(i).text());
for(int i1 = 1; i1 < trAll.size(); i1++) {
Elements td_inside = trAll.get(i1).getElementsByTag("td");
Log.d("TD INSIDE:"," " + td_inside.get(i).text());
}
}
現在我能夠獲取信息,問題是我從其他表中獲取內容,因為所有表的類名都是相同的,並且我在指定我需要的表時遇到了麻煩,並且我也正在獲取IndexOutOfBoundsException
這是此Loglink的日志
我想要的日志類型是這樣的:Hour(TIME TABLE),然后在這一小時中,我想獲取該小時的所有底線以及分鍾(TD INSIDE),然后移至下一個小時(。 )
謝謝您的光臨。
[編輯]更好的日志示例檢查第一個表。
TIME TABLE: 05H
TD INSIDE: 15
TD INSIDE: 45
TIME TABLE: 06H
TD INSIDE: 15
TD INSIDE: 35
TD INSIDE: 45
TD INSIDE: 55
TIME TABLE: 07H
TD INSIDE: 05
TD INSIDE: 15
TD INSIDE: 20
TD INSIDE: 25
TD INSIDE: 35
TD INSIDE: 40
TD INSIDE: 50
TD INSIDE: 55
(...)
你能行的:
Element table = document
.select("table.easy-table-creator:nth-child(1) tbody").first();
Elements trAll = table.select("tr");
Elements trAllBody = table.select("tr:not(:first-child)");
// For the Table Hour
Element trFirst = trAll.first();
Elements tds = trFirst.select("td");
for(int i = 0; i < tds.size(); i++){
Element td = tds.get(i);
Log.d("TIME TABLE:", " " + td.text());
String query = "td:nth-child(" + (i + 1) + ")";
Elements subTds = trAllBody.select(query);
for (int j = 0; j < subTds.size(); j++) {
Element subTd = subTds.get(j);
String tdText = subTd.text();
if(!tdText.isEmpty()){
Log.d("TD INSIDE:", " " + subTd.text());
}
}
}
一些有趣的觀點:
table.easy-table-creator:nth-child(1) tbody
選擇器正在選擇頁面中的所有表; td
: td:nth-child(index)
; trAllBody
這里包含了所有的tr
S中的不是第一個(使用tr:not(:first-child)
選擇)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.