2013-05-19 63 views
0

你好,大家好我一直有與Jsoup問題,我基本上是想從該網站獲取(解析)信息到我的應用程序 http://websemantics.co.uk/tutorials/accessibility_workshop/sessions/session2/03.data_tables/01.simple_data_tables/Jsoup的Android正在獲取信息

我想獲取考生科拉姆成數separte生物數學,科學等字符串與解析的值並附加到字符串。我該怎麼做,你可以給一個示例代碼?

我對這個問題採取將是:

//Get The Site and Parse it 
Document doc = Jsoup.connect("http://websemantics.co.uk/tutorials/accessibility_workshop/sessions/session2/03.data_tables/01.simple_data_tables/").get(); 
//Select Table 
Element table = doc.select("table").first(); 
     Iterator<Element> iterator = table.select("td").iterator(); 
     while(iterator.hasNext()){ 
      System.out.println("text : "+iterator.next().text()); 
String Math = text(); 

我嘗試了不同的方式,但不給予任何

Element table = doc.select("table").first(); 
       Iterator<Element> iterator = table.select("td").iterator(); 
       while(iterator.hasNext()){ 
        Element row1 = table.select("td:eq(1)").first(); 
        String rowno1 = row1.text(); 
               Element row2= table.select("td:eq(1)").first(); 
        String rowno2 = row2.text(); 

我不知道如何與這個進一步得到,你能解釋一下我做錯了什麼?

謝謝

回答

0

你想,不,所以你應該得到<tr> S,不<td> S中的第一個表。

table.select("td").iterator(); 

要:

所以,你的代碼更改

table.select("tr").iterator(); 

這就是它!

工作實例(我還加什麼來幫助你分配對象的變量):

public static void main(String[] args) throws Exception { 
    // Get The Site and Parse it 
    Document doc = Jsoup.connect("http://websemantics.co.uk/tutorials/accessibility_workshop/sessions/session2/03.data_tables/01.simple_data_tables/").get(); 
    // Select Table 
    Element table = doc.select("table").first(); 

    String math = ""; 
    Iterator<Element> lines = table.select("tr").iterator(); 
    while (lines.hasNext()) { 
     Element line = lines.next(); 
     System.out.println("text : "+line.text()); 
     if (line.text().contains("Mathematics")) { 
      math = line.text(); 
     } 
    } 

    System.out.println("MATH: "+math); 
} 

輸出:

text : Subject Number of candidates A B C D E U 
text : Totals 176 28 37 32 32 25 15 
text : Mathematics 36 6 7 8 6 5 4 
text : English 34 6 8 7 7 5 1 
text : Chemistry 40 6 9 9 7 5 4 
text : Physics 36 6 7 8 6 5 4 
text : Biology 30 4 6 7 6 5 2 
MATH: Mathematics 36 6 7 8 6 5 4