2016-01-13 151 views
0

我想從這個Webpage.中提取足球裝置現在我已經有了從該網頁的表中提取裝置的代碼。使用Jsoup提取特定的表數據

private class LoadFixtures extends AsyncTask<Void,Void,Void> { 
    String stringDT="",stringHome="",stringAway=""; 
    String url = "http://www.bbc.com/sport/football/spanish-la-liga/fixtures"; 
    String stringTime=""; 
    @Override 
    protected Void doInBackground(Void... params) { 
     Document doc = null; 
     try { 
      doc = Jsoup.connect(url).timeout(0).get(); 

      Elements matchDetails = doc.select("td.match-details"); 
      Elements ele_hTeam = matchDetails.select("span.team-home.teams"); 
      Elements ele_aTeam = doc.select("span.team-away.teams"); 
      Elements ele_time = doc.select("td.kickoff"); 

      int tsize = ele_hTeam.size(); 
      for(int i=0;i<tsize;i++) { 
       stringTime+="\n\n"+ele_time.get(i).text(); 
       stringHome+="\n\n"+ele_hTeam.get(i).text(); 
       stringAway+="\n\n"+ele_aTeam.get(i).text(); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     homeTeam.setText(stringHome); 
     awayTeam.setText(stringAway); 
     timeView.setText(stringTime); 

     super.onPostExecute(aVoid); 

    } 
} 

這段代碼給了我燈具的整個列表,但我想要做的只是提取特定日期的燈具。例如,可以說我想僅從提取裝置2016年1月16日星期六

回答

0

下面的代碼將按照您的要求進行操作。我只是提供了一個字符串變量和你正在查找的日期。下面的代碼在頁面上的每個表上循環。每個表格將包含x個夾具。如果表格標題包含您提供的日期,它將進入該表格並允許您選擇主隊和客隊。希望這可以幫助!

String dateLookup= "16th January 2016"; 
String url = "http://www.bbc.com/sport/football/spanish-la-liga/fixtures"; 

try { 
    Document document = Jsoup.connect(url).timeout(0).get(); 
    Elements tableElements = document.select("table.table-stats"); 
    for (Element e : tableElements) { 
     if (e.select("caption").text().contains(dateLookup)) { 
      Elements matchElements = e.select("tr.preview"); 
      for (Element match : matchElements) { 
       System.out.println("Home Team: " + match.select("span.team-home").text()); 
       System.out.println("Away Team: " + match.select("span.team-away").text() + "\n"); 
      } 
     } 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
}