【问题标题】:I have to retrieve data from html table using jsoup我必须使用 jsoup 从 html 表中检索数据
【发布时间】:2017-08-17 11:44:38
【问题描述】:

这是enter image description here的表格

我正在使用此代码

 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

但我只想要主题名称和出席率

我尝试了很多东西,但都没有得到任何结果。 实际上我想存储主题名称,参加的讲座和总讲座和百分比

这样我就可以方便地在应用程序中显示这个

例子:

我想将所有主题存储在主题[] 即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) 

【问题讨论】:

  • cols.get(2).text() 使用此link 获取更多帮助。

标签: java android jsoup


【解决方案1】:

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

System.out.println("MTTD8---&gt; "+ 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();
}    

               }

【讨论】:

  • 我想存储所有 4 个主题的值[0]=Industrial managemnet Attended[0]=29 total[0]=31 avg[0]=93.55
  • subjects[i] or subject[j] and col or cols?两者仍然给出错误
  • 我再次更新了帖子,我将每个主题存储了 4 次
  • @user7449462 已修复。我的错。 Remivet 内循环
  • 它为 col(2) 和 col(3) 提供 indexoutofbound 异常
【解决方案2】:

尝试使用这个

     for (int i = 2; i < elements.size()-1; 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;
            for (j = 2; 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();


            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多