简体   繁体   English

在Jsoup中使用href解析表类时遇到问题

[英]Trouble parsing table class with href in Jsoup

I am very new to JSOUP, have only been using it for a couple days, learning mostly from this website. 我对JSOUP还是陌生的,仅使用了几天,主要从此网站学习。 Now I'm trying to get some information from the below HTML: 现在,我正在尝试从以下HTML获取一些信息:

        <td class="day no-repetition">Sun</td>
        <td class="full-date" nowrap="nowrap">17/05/15</td>
        <td class="competition"><a href="/national/england/premier-league/20142015/regular-season/r25191/" title="Premier League">PRL</a></td>

          <td class="team team-a ">
              <a href="/teams/england/sunderland-association-football-club/683/" title="Sunderland">
                Sunderland
              </a>
          </td>

        <td class="score-time score">
          <a href="/matches/2015/05/16/england/premier-league/sunderland-association-football-club/leicester-city-fc/1704225/" class="result-draw">           
            0 - 0
          </a>
        </td>
          <td class="team team-b ">
            <a href="/teams/england/leicester-city-fc/682/" title="Leicester City">
              Leicester City
            </a>
          </td>
        <td class="events-button button first-occur">
        </td>

          <td class="info-button button">

              <a href="/matches/2015/05/16/england/premier-league/sunderland-association-football-club/leicester-city-fc/1704225/" title="More info">More info</a>

          </td>

I need to extract the Home team, score and the Away Team from the above however I am currently having issues with this. 我需要从上述数据中提取主队,得分和客队,但是我目前对此有疑问。 I need both the link and the text itself. 我需要链接和文本本身。 Below is the code I have: 下面是我的代码:

     try {
        Document doc = Jsoup.connect(URL).get();
        Element table = doc.select("table[class=matches]").first();
        Elements rows = table.select("tr");
        for (int i=0; i<rows.size(); i++){
            Element row = rows.get(i);
            Elements data = row.select("td[class=team.team-a]");

            System.out.println(data.text());

        }


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

This hasn't worked so far. 到目前为止还没有成功。 I tried 'team.team-a', 'team.team.a' and all other variations of it. 我尝试了“ team.team-a”,“ team.team.a”及其所有其他变体。 I managed to get the data that's in the 'competition' class, which works when I just replace ("td[class=team.team=a]") with (td[class=competition]) however this doesn't work with any of the classes that have a link. 我设法获取了“竞争”类中的数据,当我仅将(“ td [class = team.team = a]”)替换为(td [class = competition])时,该类就可以使用,但是不适用于任何具有链接的类。

Assistance would be highly appreciated! 协助将不胜感激!

Just separate multiple classes with a dot: 只需用一个点分隔多个类:

td.team.team-a > a  // first team
td.team.team-b > a  // second team
td.score > a  // score

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

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