0

大家好,我一直遇到 Jsoup 问题,我基本上是在尝试从该站点获取(解析)信息到我的应用程序 http://websemantics.co.uk/tutorials/accessibility_workshop/sessions/session2/03.data_tables/01。 simple_data_tables/

我想将 Number of Candidates 列提取到生物数学、科学等的单独字符串中,并将值解析并附加到字符串中。我该怎么做,你能给出一个示例代码吗?

我对此的看法是:

//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();

我不知道如何进一步解决这个问题,你能解释一下我做错了什么吗?

谢谢你

4

1 回答 1

0

你想要lines,而不是columns,所以你应该得到第一个表<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
于 2013-05-19T22:47:25.933 回答