简体   繁体   English

jSoup获取表中每个元素的链接

[英]jSoup get link of each element in table

I am trying to get the link to every team in the table on http://www.statto.com/football/stats/england/premier-league . 我正在尝试获取http://www.statto.com/football/stats/england/premier-league上表中每个团队的链接。 Currently my code only gets the team names, but seems to output every team as one string... I would like each element to be output as the link, so "Chelsea" would be " http://www.statto.com/football/teams/chelsea ". 目前,我的代码仅获取团队名称,但似乎将每个团队输出为一个字符串...我希望将每个元素输出为链接,因此“切尔西”应为“ http://www.statto.com/足球/球队/切尔西 ”。

My current code: 我当前的代码:

        Document doc = Jsoup.connect(
            "http://www.statto.com/football/stats/england/premier-league").get();

    Element tableHeader = doc.select("table[class=tabBG]").first();
    for (Element element : tableHeader.children()) {
        // Here you can do something with each element
            String team = element.select("td:eq(1) a").text();
            System.out.println(team);

        }
    }

Does anybody know how I can get the link to each item in the table to output as individual strings? 有人知道我如何获得表中每个项目的链接以单独的字符串形式输出吗?

Thanks, 谢谢,

Rob

I have now worked out a solution to my problem, below is the code that works: 现在,我已经解决了我的问题,下面是有效的代码:

Document doc = Jsoup.connect(
            "http://www.statto.com/football/stats/germany/bundesliga").get();

    Element tableHeader = doc.select("tbody").first();
    for (Element element : tableHeader.children()) {
        // Here you can do something with each element
        if (element.select("td:eq(1)").html().contains("acronym") != false || element.select("td:eq(1)").html().contains("nbsp") != false){
            //do nothing
        } else {
            String teamname = element.select("td:eq(1) a").html();
            String team = element.select("td:eq(1)").toString()
                    .replace("<a href=\"", "http://www.statto.com").replace(" class=\"b\">", "").replace(teamname, "").replace("<td class=\"steam\">", "").replace("\"</a></td>", "");


            System.out.println(team);

        }

    }

Which gives me the below output: 这给了我下面的输出:

http://www.statto.com/football/teams/bayern-munich
http://www.statto.com/football/teams/vfl-wolfsburg
http://www.statto.com/football/teams/borussia-monchengladbach
http://www.statto.com/football/teams/hannover-96
http://www.statto.com/football/teams/tsg-hoffenheim
http://www.statto.com/football/teams/bayer-leverkusen
http://www.statto.com/football/teams/augsburg
http://www.statto.com/football/teams/mainz
http://www.statto.com/football/teams/paderborn-07
http://www.statto.com/football/teams/fc-cologne
http://www.statto.com/football/teams/schalke-04
http://www.statto.com/football/teams/eintracht-frankfurt
http://www.statto.com/football/teams/sc-freiburg
http://www.statto.com/football/teams/hertha-bsc-berlin
http://www.statto.com/football/teams/werder-bremen
http://www.statto.com/football/teams/sv-hamburg
http://www.statto.com/football/teams/vfb-stuttgart
http://www.statto.com/football/teams/borussia-dortmund

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM