使用Jsoup提取特定的表数据

时间:2016-01-13 17:14:46

标签: android web-scraping jsoup

我正试图从这个Webpage.中提取足球装置现在我已经使用此代码从该网页的表格中提取装置。

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日周六提取灯具

1 个答案:

答案 0 :(得分:0)

以下代码将按您的要求执行。我只提供了一个String变量,其中包含您正在查找的日期。下面的代码在页面上的每个表上循环。每个表格将包含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();
}