我正试图抓住这个网页http://www.skysports.com/football/competitions/la-liga/table.I只想要表格中的团队名称。我正在为此目的使用Jsoup。这是我的代码
private class LoadData extends AsyncTask<Void,Void,Void> {
String url = "http://www.skysports.com/football/competitions/la-liga/table";
String data = "";
@Override
protected Void doInBackground(Void... params) {
Document document;
try {
document = Jsoup.connect(url).timeout(0).get();
Elements clubName = document.select("td.standing-table__cell standing-table__cell--name");
int a = clubName.size();
for(int i = 0; i < a; i++) {
data += "\n\n" +clubName.get(i).text();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
teamview = (TextView) findViewById(R.id.club_view);
teamview.setMovementMethod(new ScrollingMovementMethod());
teamview.setText(data);
super.onPostExecute(result);
}
}
这是它的HTML代码
<tr class="standing-table__row" data-item-id="872">
<td class="standing-table__cell">1</td>
<td class="standing-table__cell standing-table__cell--name" data-short-name="Atletico Madrid" data-long-name="Atletico Madrid">
<a href="/football/teams/atletico-madrid" class="standing-table__cell--name-link">Atletico Madrid</a>
</td>
<td class="standing-table__cell">19</td>
<td class="standing-table__cell is-hidden--bp35">14</td>
<td class="standing-table__cell is-hidden--bp35">2</td>
<td class="standing-table__cell is-hidden--bp35">3</td>
<td class="standing-table__cell is-hidden--bp35">27</td>
<td class="standing-table__cell is-hidden--bp35">8</td>
<td class="standing-table__cell">19</td>
<td class="standing-table__cell" data-sort-value="1">44</td>
<td class="standing-table__cell is-hidden--bp15 is-hidden--bp35 " data-sort-value="15333033">
<div class="standing-table__form">
<span title="Granada 0-2 Atletico Madrid" class="standing-table__form-cell standing-table__form-cell--win"> </span><span title="Atletico Madrid 2-1 Athletic Bilbao" class="standing-table__form-cell standing-table__form-cell--win"> </span><span title="Malaga 1-0 Atletico Madrid" class="standing-table__form-cell standing-table__form-cell--loss"> </span><span title="Rayo Vallecano 0-2 Atletico Madrid" class="standing-table__form-cell standing-table__form-cell--win"> </span><span title="Atletico Madrid 1-0 Levante" class="standing-table__form-cell standing-table__form-cell--win"> </span><span title="Celta Vigo 0-2 Atletico Madrid" class="standing-table__form-cell standing-table__form-cell--win"> </span> </div>
</td>
</tr>
当我使用代码document.select("td.standing-table__cell");
时,会显示数据。但是当我使用document.select("td.standing-table__cell standing-table__cell--name");
代替document.select("td.standing-table__cell");
时,没有显示数据!?
答案 0 :(得分:1)
选择器document.select("td.standing-table__cell standing-table__cell--name");
将选择标记名为standing-table__cell--name
的所有元素,以及具有名为standing-table__cell
的类的td元素的(间接)子元素。没有这样的元素存在,因此Jsoup返回一个空列表。
您可能希望选择同时包含standing-table__cell
和standing-table__cell--name
类的td元素。这可以通过CSS选择器完成,如下所示:
document.select("td.standing-table__cell.standing-table__cell--name");
注意:后跟类名的点是类的CSS选择器。它们可以连接起来。
答案 1 :(得分:0)
下面的代码循环遍历表格的每一行。然后,它根据css类名称打印出for循环所在表格行中的俱乐部名称。
String url = "http://www.skysports.com/football/competitions/la-liga/table";
try {
Document document = Jsoup.connect(url).timeout(0).get();
Elements clubRow = document.select("tr.standing-table__row");
for(Element club: clubRow) {
System.out.println(club.select("a.standing-table__cell--name-link").text());
}
} catch (IOException e) {
e.printStackTrace();
}