限制jsoup检索的内容

时间:2013-08-22 21:16:05

标签: java android listview jsoup

我很高兴学习使用jsoup并成功检索并显示来自网站的数据,但是如果有人可以提供帮助,我现在想要一些进一步的指导。

使用下面的代码返回所有表格行30+,如何检索只说出这些行的前10行?

当返回那些行及其上的数据时,数据之间的行中有间隙/空格,行之间的空格很好,但是我想要除去的行中的空格,我怎么能省略那些空间/差距?

我的代码到目前为止......

package com.example.shiftzer;

import java.io.IOException;
import java.util.ArrayList;

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

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity{

TextView textView1;
ListView shippingList; 

  public static final String APP_PREFERENCES = "AppPrefs";
    SharedPreferences settings; 
    SharedPreferences.Editor prefEditor;

   @Override
     public void onCreate(Bundle savedInstanceState) {         
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main_activity);
        //rest of the code

       textView1 = (TextView)findViewById(R.id.textView1);
       shippingList = (ListView) findViewById(R.id.listView1);

       settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
       prefEditor = settings.edit();

       new VTSTask().execute();//starts AsyncTask in private class VTSTask to get      shipping info
    }

   private class VTSTask extends AsyncTask<Void, Void, ArrayList<String>> {
       ArrayList<String> arr_shipping=new ArrayList<String>();
        /**
         * @param args
         */
        @Override
        protected ArrayList<String>  doInBackground(Void... params) {

            Document doc;
            String shippingList;

            try {
                doc =   Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp").get(); 
                Elements tableRows = doc.select("table.dynlist tr   td");

                 for (Element element : tableRows) {
                      shippingList = element.text();
                      arr_shipping.add(shippingList);// add value to  ArrayList
                    } 
                 } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }      

            return arr_shipping;//<< Return ArrayList from here
        }

         @Override
         protected void onPostExecute(ArrayList<String> result) {        
             //TextView tVShipping= (TextView)findViewById(R.id.textView2);

             shippingList = (ListView) findViewById(R.id.listView1);
             ArrayAdapter<String> adapter = 
                 new ArrayAdapter<String>(MainActivity.this, 
                                           android.R.layout.simple_list_item_1, 
                                          android.R.id.text1);

             for (String shipping_result : result)
             {
                adapter.add(shipping_result);
             }

             // Assign adapter to ListView
             shippingList.setAdapter(adapter); 

          }
    }


}

谢谢。

编辑:

try {
                doc = Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp").get(); 
                Elements tableRows = doc.select("table.dynlist tr td");

                tableRows.size();
                        for(int i = 0; i < 10; i++){
                                  tableRows.get(i);
                   shippingList  = tableRows.get(i).text() +"\n";

                      arr_shipping.add(shippingList);// add value to ArrayList
                    } 
                 } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }      

            return arr_shipping;//<< return ArrayList from here
        }

3 个答案:

答案 0 :(得分:2)

Elements没有使用(Element element:tableRows),而是使用size方法。

所以,您应该能够只使用大小进行一些验证,然后只需

for(int i = 0; i < 10; i++){
  tableRows.get(i);
} 

获得10个。

至于空格,在将它们存储在你的arraylist中之前,只需使用正则表达式并删除空格。

http://www.vogella.com/articles/JavaRegularExpressions/article.html

答案 1 :(得分:1)

试一下

   import java.io.IOException;
    import java.util.ArrayList;

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

    public class test
    {

         static ArrayList<String> arr_shipping=new ArrayList<String>();
     public static void main(String args[]) throws IOException
      {
         try {
            Document  doc = Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp").timeout(600000).get(); 
             Elements tableRows = doc.select("table.dynlist tr:not(:eq(0))");

             tableRows.size();
                     for(int i = 0; i < 10; i++){
                               //tableRows.get(i);
              String  shippingList =tableRows.get(i).text() +"\n";

                   arr_shipping.add(shippingList);// add value to ArrayList
                   System.out.println(shippingList);
                 } 
              } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }      

       //  return arr_shipping;//<< return ArrayList from here

      }

    }

答案 2 :(得分:0)

试试这个

doc.select("table.dynlist tr:lt(10)");

限制结果。

Reference