如何使用 JSOUP 提取 table

How can I extract table with JSOUP

我正在编写一个 Android 应用程序并试图弄清楚我应该如何构建我的调用以从此网页获取 table 数据:http://uk.soccerway.com/teams/scotland/saint-mirren-fc/1916/squad/

我已经从 JSOUP 网站上阅读了食谱,但是因为在我有点卡住之前我还没有使用过这个库。我想到了这样的事情:

doc = Jsoup.connect("http://uk.soccerway.com/teams/scotland/saint-mirrenfc/1916/squad/").get();
Element squad = doc.select("div.squad-container").first(); Element
Elements table = squad.select("table squad sortable");

如您所见,我离获得玩家统计数据还很远。我认为下一步应该是将新的 Element 对象指向 "table squad sortable" 内的 "tbody" 标记? 我知道一旦我设法读取 table 然后读取循环内的每一行,我将不得不使用 for 循环。

不幸的是,table结构对于没有经验的人来说有点复杂,所以我非常感谢您的建议!

基本上每一行都有以下选择器-
#page_team_1_block_team_squad_3-table > tbody:nth-child(2) > tr:nth-child(X) 其中 X 是行号(从 1 开始)。
一种方法是遍历行并提取信息:

String url = "http://uk.soccerway.com/teams/scotland/saint-mirren-fc/1916/squad/";
String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0";
Document doc = null;
try {
    doc = Jsoup.connect(url)
            .userAgent(userAgent)
            .get();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
int i = 1;
Elements row;
do {
    row = doc.select("#page_team_1_block_team_squad_3-table > tbody:nth-hild(2) > tr:nth-child(" + i + ")");
    for (Element el : row) {
        System.out.print(el.select(".shirtnumber").text() + " ");
        System.out.println(el.select(".name").text());
        i++;
    }
} while (row != null); 

这将打印每个玩家的号码和姓名。由于我不想计算行数(并保持程序对更改灵活),我建议使用 do...while 循环 - 我将在行存在(或不为空)时迭代 ling。
我得到的输出:

1 J. Langfield 21 B. O'Brien 28 R. Willison 2 S. Demetriou 3 G. Irvine 4 A. Webster ...

使用浏览器的开发人员工具获取其他列的名称,并使用它获取所需的所有信息。