Ebad khan Ebad khan - 2 years ago 98
Java Question

Want to get the Title and Grade using Jsoup

I want to get the Title and Grade of the subject from a table using Jsoup. I have Successfully got the Title of the subject, but not getting the grade. I am getting the grade plus the Subject code of the subject, but I just want the grade. It will be really helpful if you guys help me solve the problem.

For Example from the first row of this table I want Operating Systems and grade B+ and so on.

Here is the table..

<table class="FullWidth gv" cellspacing="0" align="Center" rules="all" border="1" id="ctl00_Body_gvResult" style="border-collapse:collapse;">
<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>
<td class="Center">
</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">
</td><td class="Center NoWrap">CSL 330</td><td class="Left FullWidth">Operating System Lab</td><td class="Center NoWrap">B+</td>
<td class="Center">
</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">
</td><td class="Center NoWrap">SEL-206</td><td class="Left FullWidth">Database Management Systems Lab</td><td class="Center NoWrap">A</td>
<td class="Center">
</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">
</td><td class="Center NoWrap">SEN-206</td><td class="Left FullWidth">Database Management Systems</td><td class="Center NoWrap">A</td>
<td class="Center">
</td><td class="Center NoWrap">SEN-217</td><td class="Left FullWidth">Data Structures &amp; Algorithms</td><td class="Center NoWrap">B</td>


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 = "";
HashMap<String, String> hashMaps;

protected void onCreate(Bundle savedInstanceState) {

hashMaps = Data.map;

new results().execute();


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

String data;
String data2;

protected void onPreExecute() {


protected Void doInBackground(Void... params) {

try {
Document doces = Jsoup.connect(urlws)

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

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

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

data2 = element.ownText();


} catch (Exception e) {

return null;

protected void onPostExecute(Void aVoid) {




Answer Source

Try this:

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){
    String title = tds.get(2).ownText();

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

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

This assumes of course that in the table you always find 4 columns of which column 3 holds the course title and column 4 holds the grade. The check for tds.isEmpty() || tds.size()<4 is needed because the first row of the table holds the headers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download