0

嗨,我正在尝试从网站 (http://omhc.nl/site/default.asp?Option=10017&m=1) 获取一些信息。表结构为:

<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>

我的代码:

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];
    }
}

我想获得第[0]天:

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

和天[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

但我得到的只是一天 [0] 的 17:00 - 22:00 KM Barhoofd。如何选择正确的单元格(从第一个 tr td[width:100%] 到下一个 tr td[width:100%])?

4

1 回答 1

0

我改变了一点你的方法 getBarSchema 主要是你的选择器:

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;
}
于 2012-04-17T15:09:44.083 回答