当前位置: 首页 > 知识库问答 >
问题:

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

甘西岭
2023-03-14

这里是表格输入图像描述

我正在使用这个代码

 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

但我只想要科目名称和出勤情况

我试过很多事情,但没有任何结果。实际上我想存储的科目名称,参加的讲座和总讲座和百分比

所以我可以在应用程序中很好地显示这一点

示例:

而且

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 :

我每门功课考四遍

新更新的帖子:

     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) 

共有1个答案

孔扬
2023-03-14

试着用这个

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


            }
 类似资料:
  • 我正在使用HTTPClient连接到一个网站。以下代码片段用于此目的: 上面的代码显示了网站的html代码。此外,我只想访问代码中的一些数据,我可以使用以下代码片段使用JSoup访问这些数据: 在上面的代码中,我使用“url”直接指定了网站的url。这意味着如果我使用JSoup,我不需要HTTPClient。有没有一种方法可以将使用HTTPClient检索到的“responseBody”集成到JS

  • 问题内容: 这是我必须从中提取值5390.85,5428.15,5376.15和5413.85的HTML源。我想使用jsoup做到这一点。但是我对jsoup比较陌生(今天我开始使用它)。那我该怎么办呢? 我已经使用jsoup提取了网站的内容。但是如何提取我需要的值?提前致谢 问题答案: 尝试这样的事情: 这是打印输出:-

  • 问题内容: 我想使用JSoup-framework提取此表,以将内容保存在“表”数组中。第一个tr-tag是表头。所有以下内容(不包括在内)均描述了内容。 我已经测试了这一个和其他一些,但是我没有让它们为我工作: 使用JSoup提取HTML表内容 问题答案: 这是一些示例代码,您如何仅选择标题: 你得到… 解析 文件 :(这里是和字符集,请参阅jsoup对铁道部的相关信息文件) 解析 网站 :(不

  • 当我试图从在线URL=forexalgerie.com中的表中获取数据时,我的目标是这些值: ...似乎我的代码一切正常: 但是结果包含表中的所有内容,除了我想要的值? 怎么了?

  • 我想使用jsoup从RSS提要检索数据。我可以在所有的标签,但我不能这样做,当有内容:编码标签。请任何人帮助我如何从内容:编码标签获取数据。我的订阅源URL是https://sambad.in/feed/我的代码也是Document doc=Jsoup。解析(String.valueOf(response));元素itemElements=doc。选择(“项目”);

  • 如何从下面的html中解析出46389333? 我使用的是JSoup,显然我可以解析文本、标签和其他值。但是我不知道如何解析这个值。即使我只能解析出/exampleTweet/status/46389333,至少我可以解析这个字符串,这要容易得多。问题是,数字变了,显然例子也变了。不过,地位还是存在的,所以也许我可以利用这一点?任何帮助都是巨大的。