Android 如何使用jsoup读取表中的所有元素

Android 如何使用jsoup读取表中的所有元素,android,jsoup,Android,Jsoup,我正试图用Jsoup读取HTML中表格的所有行。我的问题是我只读取了表中的一些元素,但没有读取所有元素,我不知道问题出在哪里。在桌子里面我有大约10个元素,我只能读6个,奇怪的是我不能读前三个元素,最后一个元素也是 这是我的代码: public class MainActivity extends ActionBarActivity { ListView listview; ListViewAdapter adapter; ProgressDialog mProgres

我正试图用Jsoup读取HTML中表格的所有行。我的问题是我只读取了表中的一些元素,但没有读取所有元素,我不知道问题出在哪里。在桌子里面我有大约10个元素,我只能读6个,奇怪的是我不能读前三个元素,最后一个元素也是

这是我的代码:

public class MainActivity extends ActionBarActivity {

    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String EVENTO = "evento";
    static String HORARIO = "fecha/hora";
    // URL Address
    String url = "http://www.eliberico.com/agenda";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);
        // Execute DownloadJSON AsyncTask
        new JsoupListView().execute();

    }

    // Title AsyncTask
    private class JsoupListView extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Descargando Agenda");
            // Set progressdialog message
            mProgressDialog.setMessage("Buscando...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();

            try {
                // Connect to the Website URL
                Document doc = Jsoup.connect(url).get();
                // Identify Table Class "worldpopulation"
                    for (Element table : doc.select("div[class=css-events-list]"))
                {
                    // Identify all the table row's(tr)
                    for (Element row : table.select("tr:gt(0)")) {
                        HashMap<String, String> map = new HashMap<String, String>();

                        // Identify all the table cell's(td)
                        Elements tds = row.select("td");


                        // Retrive Jsoup Elements
                        // Get the td's
                        map.put("fecha/hora", tds.get(0).text());
                        map.put("evento", tds.get(1).text());

                        arraylist.add(map);
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

}
公共类MainActivity扩展了ActionBarActivity{
列表视图列表视图;
ListViewAdapter适配器;
进程对话框;
ArrayList ArrayList;
静态字符串EVENTO=“EVENTO”;
静态字符串HORARIO=“fecha/hora”;
//网址
字符串url=”http://www.eliberico.com/agenda";
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从listview_main.xml获取视图
setContentView(R.layout.listview_main);
//执行下载JSON异步任务
新建JsoupListView().execute();
}
//标题异步任务
私有类JsoupListView扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//创建一个progressdialog
mProgressDialog=新建ProgressDialog(MainActivity.this);
//设置进程对话框标题
mProgressDialog.setTitle(“描述议程”);
//设置进程对话框消息
设置消息(“Buscando…”);
mProgressDialog.setUndeterminate(false);
//显示进度对话框
mProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//创建一个数组
arraylist=新的arraylist();
试一试{
//连接到网站URL
Document doc=Jsoup.connect(url.get();
//识别表类“世界人口”
对于(元素表:doc.select(“div[class=css事件列表]”)
{
//标识所有表行(tr)
对于(元素行:table.select(“tr:gt(0)”){
HashMap=newHashMap();
//识别所有表格单元格(td)
元素tds=行。选择(“td”);
//检索Jsoup元素
//获取td的
map.put(“fecha/hora”,tds.get(0.text());
map.put(“evento”,tds.get(1.text());
arraylist.add(map);
}
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//在listview_main.xml中找到listview
listview=(listview)findViewById(R.id.listview);
//将结果传递到ListViewAdapter.java中
adapter=新的ListViewAdapter(MainActivity.this,arraylist);
//将适配器设置为ListView
setAdapter(适配器);
//关闭progressdialog
mProgressDialog.disclose();
}
}
}
这是HTML的一部分

<div class='css-events-list'><h2>nov 2014</h2><table cellpadding="0" cellspacing="0" class="events-table" >
    <thead>
        <tr>
            <th class="event-time" width="150">Fecha/Hora</th>
            <th class="event-description" width="*">Evento</th>
        </tr>
    </thead>
    <tbody><tr>
            <td>
                11/11/2014 - 26/04/2015<br/>
                10:00 am - 5:45 pm
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/women-fashion-power-en-londres" title="Women Fashion Power en Londres">Women Fashion Power en Londres</a>
                <br/><i>Design Museum, London  London</i>
            </td>
        </tr></tbody></table><h2>ene 2015</h2><table cellpadding="0" cellspacing="0" class="events-table" >
    <thead>
        <tr>
            <th class="event-time" width="150">Fecha/Hora</th>
            <th class="event-description" width="*">Evento</th>
        </tr>
    </thead>
    <tbody><tr>
            <td>
                03/01/2015 - 12/04/2015<br/>
                Todo el día
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/la-exposicion-art-brick-llega-londres" title="La exposición Art of the Brick llega a Londres">La exposición Art of the Brick llega a Londres</a>
                <br/><i>Old Truman Brewery, London </i>
            </td>
        </tr></tbody></table><h2>feb 2015</h2><table cellpadding="0" cellspacing="0" class="events-table" >
    <thead>
        <tr>
            <th class="event-time" width="150">Fecha/Hora</th>
            <th class="event-description" width="*">Evento</th>
        </tr>
    </thead>
    <tbody><tr>
            <td>
                03/02/2015 - 09/05/2015<br/>
                12:00 am
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/almodovar-y-su-musical-en-londres" title="Almodóvar y su musical en Londres">Almodóvar y su musical en Londres</a>
                <br/><i>The Playhouse Theatre, London London</i>
            </td>
        </tr><tr>
            <td>
                04/02/2015 - 31/08/2015<br/>
                Todo el día
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/elvis-presley-llega-londres-en-forma-de-exposicion" title="Elvis Presley llega a Londres en forma de exposición">Elvis Presley llega a Londres en forma de exposición</a>
                <br/><i>O2 Arena, London London</i>
            </td>
        </tr><tr>
            <td>
                07/02/2015 - 31/03/2015<br/>
                10:00 am - 5:30 pm
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/exposicion-fotografias-de-naturaleza-salvaje" title="Exposición fotografías de naturaleza salvaje">Exposición fotografías de naturaleza salvaje</a>
                <br/><i>Natural History Museum, London  </i>
            </td>
        </tr><tr>
            <td>
                09/02/2015 - 10/04/2015<br/>
                Todo el día
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/una-exposicion-del-pintos-barroco-rubens-llega-londres" title="Una exposición del pintor barroco Rubens llega a Londres">Una exposición del pintor barroco Rubens llega a Londres</a>
                <br/><i>Royal Academy of Arts, London </i>
            </td>
        </tr><tr>
            <td>
                20/02/2015 - 02/04/2015<br/>
                Todo el día
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/arte-urbano-spanish-101" title="Arte urbano ‘Spanish 101’">Arte urbano ‘Spanish 101’</a>
                <br/><i>MYA GALLERY, London London</i>
            </td>
        </tr><tr>
            <td>
                26/02/2015 - 25/05/2015<br/>
                10:00 am - 5:30 pm
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/exposicion-witches-old-women-album" title="Exposición: The Witches and old Women Album">Exposición: The Witches and old Women Album</a>
                <br/><i>The Courtauld Gallery, London London</i>
            </td>
        </tr></tbody></table><h2>mar 2015</h2><table cellpadding="0" cellspacing="0" class="events-table" >
    <thead>
        <tr>
            <th class="event-time" width="150">Fecha/Hora</th>
            <th class="event-description" width="*">Evento</th>
        </tr>
    </thead>
    <tbody><tr>
            <td>
                06/03/2015 - 17/04/2015<br/>
                12:00 am
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/battersea-spanish-organiza-talleres-de-escritura-en-espanol" title="Battersea Spanish organiza talleres de escritura en español">Battersea Spanish organiza talleres de escritura en español</a>
                <br/><i>Battersea Business Centre, London London</i>
            </td>
        </tr><tr>
            <td>
                12/03/2015 - 21/03/2015<br/>
                10:00 am - 5:00 pm
            </td>
            <td>
                <a href="http://www.eliberico.com/eventos/roman-lokati-expone-en-la-mall-galleries" title="Roman Lokati expone en la Mall Galleries">Roman Lokati expone en la Mall Galleries</a>
                <br/><i>Mall Galleries, London London</i>
            </td>
        </tr></tbody></table>
2014年11月
费查/霍拉
艾文托
2014年11月11日-2015年4月26日
上午10:00-下午5:45
伦敦设计博物馆 ene 2015 费查/霍拉 艾文托 2015年1月3日-2015年4月12日
托多艾尔迪亚酒店
伦敦老杜鲁门啤酒厂 2015年2月 费查/霍拉 艾文托 2015年2月3日-2015年5月9日
上午12:00
伦敦剧场 2015年2月4日至2015年8月31日
托多艾尔迪亚酒店
伦敦O2竞技场 2015年2月7日至2015年3月31日
上午10:00至下午5:30
伦敦自然历史博物馆 2015年2月9日-2015年4月10日
托多艾尔迪亚酒店
伦敦皇家艺术学院 2015年2月20日-2015年4月2日
托多艾尔迪亚酒店
伦敦MYA画廊 2015年2月26日-2015年5月25日
上午10:00至下午5:30
伦敦考托美术馆 2015年3月 费查/霍拉 艾文托 2015年3月6日-2015年4月17日
上午12:00
伦敦巴特西商务中心 2015年3月12日-2015年3月21日
上午10:00至下午5:00
伦敦购物中心画廊
表。选择(“tr:gt(0)”)
可能不会执行您认为它会执行的操作

如果你想避免

tr
thead
中,但只想从
tbody<thead>
    <tr>
        <th class="event-time" width="150">Fecha/Hora</th>
        <th class="event-description" width="*">Evento</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>
            11/11/2014 - 26/04/2015<br/>
            10:00 am - 5:45 pm
        </td>
        <td>
            <a href="http://www.eliberico.com/eventos/women-fashion-power-en-londres" title="Women Fashion Power en Londres">Women Fashion Power en Londres</a>
            <br/><i>Design Museum, London  London</i>
        </td>
    </tr>
for (Element table : doc.select("div[class=css-events-list]"))
{
    // Identify all the table row's(tr)
    for (Element row : table.select("tr:gt(0)")) {
for (Element table : doc.select("div[class=css-events-list] > table > tbody")) 
{
    for (Element row : table.select("tr")) {