想要使用Jsoup获得标题和成绩

时间:2015-08-29 14:43:38

标签: java android html css jsoup

我希望使用Jsoup从表中获取主题的标题成绩。我已成功获得该主题的标题,但未获得成绩。我正在获得该科目的成绩加上主题代码,但我只想要成绩。如果你们帮我解决问题,那将会非常有帮助。

例如,从此表的第一行开始,我想要操作系统,并且 B + 等等。

这是表..

<table class="FullWidth gv" cellspacing="0" align="Center" rules="all" border="1" id="ctl00_Body_gvResult" style="border-collapse:collapse;">
        <tr>
            <th class="Center" scope="col">S#</th><th class="Center" scope="col">Code</th><th class="Center" scope="col">Title</th><th class="Center" scope="col">Grade</th>
        </tr><tr>
            <td class="Center">
                    1
                </td><td class="Center NoWrap">CSC 330</td><td class="Left FullWidth">Operating Systems</td><td class="Center NoWrap">B+</td>
        </tr><tr class="Alternating">
            <td class="Center">
                    2
                </td><td class="Center NoWrap">CSL 330</td><td class="Left FullWidth">Operating System Lab</td><td class="Center NoWrap">B+</td>
        </tr><tr>
            <td class="Center">
                    3
                </td><td class="Center NoWrap">GSC-205</td><td class="Left FullWidth">Complex Variables &amp; Transforms</td><td class="Center NoWrap">A</td>
        </tr><tr class="Alternating">
            <td class="Center">
                    4
                </td><td class="Center NoWrap">SEL-206</td><td class="Left FullWidth">Database Management Systems Lab</td><td class="Center NoWrap">A</td>
        </tr><tr>
            <td class="Center">
                    5
                </td><td class="Center NoWrap">SEL-217</td><td class="Left FullWidth">Data Structures &amp; Algorithms Lab</td><td class="Center NoWrap">A</td>
        </tr><tr class="Alternating">
            <td class="Center">
                    6
                </td><td class="Center NoWrap">SEN-206</td><td class="Left FullWidth">Database Management Systems</td><td class="Center NoWrap">A</td>
        </tr><tr>
            <td class="Center">
                    7
                </td><td class="Center NoWrap">SEN-217</td><td class="Left FullWidth">Data Structures &amp; Algorithms</td><td class="Center NoWrap">B</td>
        </tr>
    </table>

Result.java

package com.example.ebad.apppw;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;

public class Result extends AppCompatActivity {

    String urlws = "http://111.68.99.8/StudentProfile/Result.aspx";
    HashMap<String, String> hashMaps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_layout);

        hashMaps = Data.map;

        new results().execute();


    }


    private class results extends AsyncTask<Void, Void, Void> {

        String data;
        String data2;

        @Override
        protected void onPreExecute() {
            //super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                Document doces = Jsoup.connect(urlws)
                        .cookies(hashMaps)
                        .get();


                Elements elements = doces.select("table#ctl00_Body_gvResult").first().getElementsByTag("td");

                for (Element element : elements) {
                    if (element.className().contains("Left")) {
                        data = element.ownText();
                        System.out.println(data);
                    }
                }

                for (Element element : elements) {
                    if (element.className().contains("Center")) {
                        if (element.className().endsWith("NoWrap")) {

                            data2 = element.ownText();
                            System.out.println(data2);

                        }
                    }
                }



            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPreExecute();

        }


    }


}

3 个答案:

答案 0 :(得分:1)

不是最优雅的方式:

Document doces = Jsoup.parse(html);
String query = "#ctl00_Body_gvResult > tbody:nth-child(1) > tr:nth-child(%d) > td:nth-child(%d)";
for (int i = 1; i <= 8; i++) {
    System.out.print(doces.select(String.format(query, i, 3)).text());
    System.out.print(" ");
    System.out.println(doces.select(String.format(query, i, 4)).text());
}

答案 1 :(得分:1)

试试这个:

Document doc = Jsoup.parse(html,"");

Element tableEl = doc.select("table#ctl00_Body_gvResult").first();
Elements rowEls = tableEl.select("tr");

for (Element rowEl : rowEls){
    Elements tds = rowEl.select("td");
    if (tds.isEmpty() || tds.size()<4){
        continue;
    }
    //title
    String title = tds.get(2).ownText();

    //grade
    String grade = tds.get(3).ownText();

    System.out.println("title: "+title+", grade: "+grade);
}

当然,假设您在表格中总是找到4列,其中第3列包含课程标题,第4列包含等级。需要检查tds.isEmpty() || tds.size()<4,因为表的第一行包含标题。

答案 2 :(得分:0)

Jsoup有一个丰富的selector syntax,您可以使用它来优化您的查询。您可以使用兄弟选择器语法来获取表的特定列。这里是如何在select语句中获取表的列 -

        Elements titles = doces.select("table#ctl00_Body_gvResult td:eq(2)");

        for (Element element : titles) {
            data = element.ownText();
            System.out.println(data);
        }

        Elements grades = doces.select("table#ctl00_Body_gvResult td:eq(3)");
        for (Element element : grades) {
            data2 = element.ownText();
            System.out.println(data2);
        }

或者您可以在同一结果中交错显示两个列 -

       Elements elements = doces.select("table#ctl00_Body_gvResult td:eq(2), table#ctl00_Body_gvResult td:eq(3)");

        for (int i = 0; i < elements.size(); i = i+2) {
            String title = elements.get(i).ownText();
            String grade = elements.get(i+1).ownText();
            System.out.println(title + " : " + grade);
        }