我必须使用jsoup从html表中检索数据

问题描述:

这是表格在此处输入图像描述

我正在使用此代码

 Document doc  = Jsoup.parse(s);

       Elements elements=doc.select("table#table1").select("tbody").select("tr");


        for (int i = 0; i < elements.size(); i++) { 
            Elements row = elements.get(i).getElementsByClass("MTTD8");
            Elements cols = row.select("td");
            System.out.println("MTTD8--->  "+ row.text().toString());
           }

我收到此输出:

     MTTD8--->  
03-24 19:15:57.512 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  
03-24 19:15:57.515 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  INDUSTRIAL MANAGEMENT 31 29 93.55
03-24 19:15:57.517 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  MINI PROJECT AND SEMINAR 0 0 0
03-24 19:15:57.519 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  MINI PROJECT AND SEMINAR LAB 16 16 100
03-24 19:15:57.521 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  ANTENA AND WAVE PROPAGATION 39 38 97.44
03-24 19:15:57.523 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  EMBEDDED PROCESSORS 34 29 85.29
03-24 19:15:57.524 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  INFORMATION THEORY AND CODING TECHNIQUES 42 30 71.43
03-24 19:15:57.526 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  POWER ELECTRONICS 23 15 65.22
03-24 19:15:57.528 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  POWER ELECTRONICS LAB 12 11 91.67
03-24 19:15:57.529 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  ANTENA AND WAVE PROPAGATION LAB 10 9 90
03-24 19:15:57.531 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  INFORMATION THEORY AND CODING TECHNIQUES LAB 11 10 90.91
03-24 19:15:57.532 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  EMBEDDED PROCESSORS LAB 9 9 100
03-24 19:15:57.535 20390-20536/com.example.rushabh123453.attendance I/System.out: MTTD8--->  Average : 88.55

但是我只想要主题名称和出勤

But i want only the subject name and attendance

我尝试了很多事情,但没有得到任何结果. 实际上我想存储主题名称,出席的讲座以及总讲座和百分比

I have tried lot things but dint get any results. actually i want to store the name of subject ,attended lecture and total lecture and percentage

以便我可以方便地在应用程序中显示此内容

so that i can show conviniently show this in app

示例:

我想将所有主题存储在subject []中 即subject[0]=Industrial managemnet

I want to store all subjects in subject[] i.e subject[0]=Industrial managemnet

attended[0]=29

total[0]=31

avg[0]=93.55

更新后的帖子:-

添加此代码后

       for (int i = 0; i < elements.size(); i++) { //first row is the col names so skip it.
            Elements row = elements.get(i).getElementsByClass("MTTD8");
            Elements cols = row.select("td");
             t=row.text().toString();

             ss1.append(t);

int j=0;
            for (j = 0; j < cols.size(); j++) {
               subjects[i] = cols.get(0).text().toString();


                System.out.println("subjects  "+ subjects[i] );
            }



        }

我得到的输出是:-

03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INDUSTRIAL MANAGEMENT
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INDUSTRIAL MANAGEMENT
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INDUSTRIAL MANAGEMENT
03-24 20:41:11.928 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INDUSTRIAL MANAGEMENT
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR
03-24 20:41:11.930 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.934 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR LAB
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION
03-24 20:41:11.936 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION
03-24 20:41:11.938 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS
03-24 20:41:11.938 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS
03-24 20:41:11.939 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS
03-24 20:41:11.939 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS
03-24 20:41:11.940 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.941 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.941 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.941 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES
03-24 20:41:11.946 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS
03-24 20:41:11.946 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS
03-24 20:41:11.946 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS
03-24 20:41:11.947 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS
03-24 20:41:11.948 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS LAB
03-24 20:41:11.948 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS LAB
03-24 20:41:11.948 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS LAB
03-24 20:41:11.949 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS LAB
03-24 20:41:11.950 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.950 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.950 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.951 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION LAB
03-24 20:41:11.952 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.952 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.953 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.953 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 20:41:11.954 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS LAB
03-24 20:41:11.954 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS LAB
03-24 20:41:11.954 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS LAB
03-24 20:41:11.955 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS LAB
03-24 20:41:11.956 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  Average :
03-24 20:41:11.956 17862-18535/com.example.rushabh123453.attendance I/System.out: subjects  Average :

每个科目我要学习4次

新的更新后的帖子:

     for (int i = 0; i < elements.size(); i++) { //first row is the col names so skip it.
            Elements row = elements.get(i).getElementsByClass("MTTD8");
            Elements cols = row.select("td");
             t=row.text().toString();
           // textView.setText(t);
           // System.out.println("MTTD8--->  "+t );
             ss1.append(t);

            //System.out.println("MTTD8cols--->  "+ cols.toString());



int j=0;
            for (j = 0; j < cols.size(); j++) {
               subjects[i] = cols.get(0).text().toString();
             total[i] = cols.get(2).text().toString();
                attended[i] = cols.get(1).text().toString();
                  avg[i] = cols.get(3).text().toString();


            }

            System.out.println("subjects  "+ subjects[i] );
            System.out.println("subjects  "+ attended[i] );
            System.out.println("subjects  "+ total[i] );
            System.out.println("subjects  "+ avg[i]);
        }







        return null;
    }

输出:-

03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.394 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.395 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.395 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.395 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.396 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  null
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  INDUSTRIAL MANAGEMENT
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  31
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  29
03-24 21:09:09.398 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  93.55
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  0
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  0
03-24 21:09:09.400 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  0
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  MINI PROJECT AND SEMINAR LAB
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  16
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  16
03-24 21:09:09.402 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  100
03-24 21:09:09.403 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION
03-24 21:09:09.403 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  39
03-24 21:09:09.403 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  38
03-24 21:09:09.404 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  97.44
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  34
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  29
03-24 21:09:09.406 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  85.29
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  42
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  30
03-24 21:09:09.408 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  71.43
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  23
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  15
03-24 21:09:09.411 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  65.22
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  POWER ELECTRONICS LAB
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  12
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  11
03-24 21:09:09.413 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  91.67
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  ANTENA AND WAVE PROPAGATION LAB
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  10
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  9
03-24 21:09:09.415 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  90
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  INFORMATION THEORY AND CODING TECHNIQUES LAB
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  11
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  10
03-24 21:09:09.419 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  90.91
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  EMBEDDED PROCESSORS LAB
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  9
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  9
03-24 21:09:09.421 15310-17183/com.example.rushabh123453.attendance I/System.out: subjects  100
03-24 21:09:09.430 15310-17183/com.example.rushabh123453.attendance E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                                                                                      Process: com.example.rushabh123453.attendance, PID: 15310
                                                                                      java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                          at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                                          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                          at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                          at java.lang.Thread.run(Thread.java:818)
                                                                                       Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
                                                                                          at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                          at java.util.ArrayList.get(ArrayList.java:308)
                                                                                          at org.jsoup.select.Elements.get(Elements.java:544)
                                                                                          at com.example.rushabh123453.attendance.MainActivity.getjson(MainActivity.java:188)
                                                                                          at com.example.rushabh123453.attendance.MainActivity.access$000(MainActivity.java:40)
                                                                                          at com.example.rushabh123453.attendance.MainActivity$Networking1.doInBackground(MainActivity.java:126)
                                                                                          at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                          at java.lang.Thread.run(Thread.java:818) 

System.out.println("MTTD8---> "+ col.get(0).text().toString());打印第一个td值.

System.out.println("MTTD8---> "+ col.get(2).text().toString());以打印出勤率td值.

编辑

    for (int i = 0; i < elements.size(); i++) { 
                Elements row = elements.get(i).getElementsByClass("MTTD8");
                Elements cols = row.select("td");
  if (cols.lenght()  ==4) {
    subjects[i] = cols.get(0).text().toString();
    total[i] = cols.get(1).text().toString();
    attended[i] = cols.get(2).text().toString();
    avg[i] = cols.get(3).text().toString();
}    

               }