2013-08-22 44 views
0

我很開心學習使用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 
     } 
+0

你是什麼意思用空格/空白?  ? – Niranjan

+0

是的這是正確的 –

回答

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 

     } 

    } 
+0

嗨,謝謝你的回答,我已經接受了,你可以進一步解釋這裏發生了什麼:doc.select(「table.dynlist tr:not :當量(0))「); ?謝謝。 –

+0

am選擇等於「dynlist」的表類,所以它給我你想要的表然後「tr:not(:eq(0)」顯示除第一行以外的所有行,這就像頭/瓦和東西肯定你不需要,很高興它幫助了你,感謝你將它標記爲答案。 – ImGeorge

2

而不是做爲(元素元素:tableRows)的,元件的尺寸的方法。

所以,你應該能夠只是做一些驗證與大小,然後簡單地

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

,讓他們的10。

至於空格,在將它們存儲在您的數組列表之前,只需使用正則表達式並刪除空格。

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

+0

Alexander Lin。嗨,我正在嘗試您的建議,並已編輯我的問題與我目前爲止,但它仍然返回整個列表可以指出任何錯誤嗎? –

0

試試這個

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

到LIMT結果。

Reference

+0

嗨,感謝您的幫助,但沒有這不起作用 –