2012-05-24 66 views
0
static public void main(String[] args) { 
    Document playerpage = 
     Jsoup.connect("http://en.wikipedia.org/wiki/Diego_Forl%C3%A1n").get(); 
    Elements table = playerpage.getElementsByTag("table").select("table.infobox"); 
    Elements ind = table.select("tr:contains(Club information)"); 

如您所見,我成功選擇了包含文本的表格行:Club information在jsoup中查找所選行元素的行索引

如何返回此元素的行索引,因爲我想將該數字用於下一個搜索?

回答

1
Document playerpage = Jsoup.connect("http://en.wikipedia.org/wiki/Diego_Forl%C3%A1n").get(); 
    Elements table = playerpage.select("table.infobox > tbody > tr"); 
    int rowCount = 0; 
    for (Element e : table) { 
     if (e.text().contains("Club information")) { 
      System.out.println("Row: " + rowCount); 
      System.out.println(e); 
     } 
     rowCount++; 
    } 
+0

非常感謝。我會盡我所能地讓它工作,但你的回答很有幫助。 –