2013-05-19 64 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