简体   繁体   中英

Android jsoup select table cells

Hi i'm trying to get some information from a website (http://omhc.nl/site/default.asp?Option=10017&m=1). The table structure is:

<tr>
    <td colspan="4" style="border-bottom: 1px solid rgb(0, 0, 0);" width="100%">donderdag 19 april 2012&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">17:00 - 22:00&nbsp;</td>
    <td bgcolor="" width="40%">KM</td>
    <td width="6%">Barhoofd&nbsp;</td>
</tr>
<tr>
    <td colspan="4" style="border-bottom: 1px solid rgb(0, 0, 0);" width="100%">vrijdag 20 april 2012&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">16:30 - 19:30&nbsp;</td>
    <td bgcolor="" width="40%">Ouders/verzorgers van VL</td>
    <td width="6%">Bardienst&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">16:30 - 19:30&nbsp;</td>
    <td bgcolor="" width="40%">Ouders/verzorgers van AvdN</td>
    <td width="6%">Bardienst&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">16:30 - 21:00&nbsp;</td>
    <td bgcolor="" width="40%">EdK</td>
    <td width="6%">Barhoofd&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">21:00 - 23:00&nbsp;</td>
    <td bgcolor="" width="40%">FK</td>
   <td width="6%">Barhoofd&nbsp;</td>
</tr>
<tr>
    <td width="5%">&nbsp;</td>
    <td width="25%">23:00 - 00:00&nbsp;</td>
    <td bgcolor="" width="40%">SW</td>
    <td width="6%">Barhoofd&nbsp;</td>
</tr>

My code:

package maartenbrakkee.bardienst.omhc;

import java.util.List;

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

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class BardienstActivity extends Activity {
    TextView tv;
    static final String URL = "http://omhc.nl/site/default.asp?Option=10017&m=1";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView)findViewById(R.id.tv);
        tv.setSingleLine(false);

        try {
            tv.setMovementMethod(new ScrollingMovementMethod());
            tv.setText(getBarschema());
        } catch (Exception ex) {
            tv.setText("Error");
        }
    }

    protected String getBarschema() throws Exception {
        String result = "";
        Document document = Jsoup.connect(URL).get();

        Elements dagen = document.select("tr:has(td[width=100%]) + tr:has(td[width=40%])");

        // Dag
        String[] dag = new String[dagen.size()];
        int i = 0;
        for (Element dagen1 : dagen) {
            dag[i++] = dagen1.text() + "\n";
        }

        return dag[3];
    }
}

I'd like to get for day[0]:

donderdag 19 april 2012 + "\n" + 17:00 - 22:00 KM Barhoofd

and day[1]:

vrijdag 20 april 2012 + "\n" + 16:30 - 19:30  Ouders/verzorgers van VL Bardienst "\n" + 16:30 - 19:30 Ouders/verzorgers van AvdN Bardienst + "\n" + 16:30 - 21:00 EdK Barhoofd + "\n" + 21:00 - 23:00   FK Barhoofd + "\n" 23:00 - 00:00 SW Barhoofd

but all i get is only 17:00 - 22:00 KM Barhoofd for day[0]. How can select the right cells (from the first tr td[width:100%] till the next tr td[width:100%])?

I change a little bit your method getBarSchema and mainly your selector:

static protected List<String> getBarschema(String URL) throws Exception {

    Document document = Jsoup.connect(URL).get();

    // New Selector
    Elements dagen = document.select("div.content table tr td");

    // List better than array in this case
    List<String> dag = new ArrayList();   

    String line = "";
    for (Element dagen1 : dagen) {                              

        String width = dagen1.attr("width");      
        if(width.equals("100%") && !line.equals("")){
            dag.add(line);
            line ="";
        }    
        line += dagen1.text() + "\n";
    }        
    return dag;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM