Jsoup:具有类名的表

时间:2018-03-12 15:21:55

标签: java html jsoup

我最近开始使用Jsoup库,并且在访问类和打印已解析的代码时遇到了一些问题。我想打印出团队名称(“NYE”)和胜利数量(14),我尝试了多种方法来执行这个问题,包括使用getElementsByClass,getElementsByTag,select和其他几个方法,但还没有任何产生输出的运气。任何帮助将不胜感激。

<div class="table-responsive">
    <table class="table table-striped table-condensed u-verticalPadding--x-small ScrollArea-content">
        <thead>
            <tr>
                <th/>
                <th class="Standings-header-team"/>
                <th class="Standings-header" title="Division">DIV</th>
                <th class="Standings-header" title="Matches Played">MP</th>
                <th class="Standings-header" title="Match Wins">W</th>
                <th class="Standings-header" title="Match Losses">L</th>
                <th class="Standings-header" title="Maps Won, Lost, Tied">Map W-L-T</th>
                <th class="Standings-header" title="Map Differential">DIFF</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <b>1</b>
                </td>
                <td class="Standings-details-team">
                    <div class="IconLabel">
                        <span class="IconLabel-item">
                            <img src="https://bnetcmsus-a.akamaihd.net/cms/page_media/3PBR8VEYM8SH1517250447953.svg" alt="New York Excelsior" class="Icon">
                            </span>
                            <span class="IconLabel-item hidden-xs">
                                <div>New York Excelsior</div>
                            </span>
                            <span class="IconLabel-item hidden-sm hidden-md hidden-lg hidden-xl" title="New York Excelsior">
                                <div>NYE</div>
                            </span>
                        </div>
                    </td>
                    <td class="Standings-details-division">ATL</td>
                    <td class="Standings-details">16</td>
                    <td class="Standings-details">14</td>
                    <td class="Standings-details">2</td>
                    <td class="Standings-details">51-15-2</td>
                    <td class="Standings-details is-positive">+36</td>
                </tr>

2 个答案:

答案 0 :(得分:0)

经过一番挖掘,看起来有一个脚本可以加载表格,这是我的猜测

<section class="Section  Section--no-sides Section--thin-top">
  <div class="container">
   <div id="standings"></div>
  </div>
</section>

这就是jsoup为您提供的那个试图从

拉出表格的部分

如果解析正确,请查看此https://api.overwatchleague.com/standings?expand=team.content&locale=en_US,它将为您提供表格中的所有信息以及更多信息

答案 1 :(得分:0)

以下代码应该能够解析您要查找的数据。我在代码中添加了注释,以帮助您了解每个步骤的作用。

assy

输出:

// Use CSS selectors to select the table header elements that correspond
// to the table data rows we want to select (i.e. team name and wins)
Element teamNameElement = doc.select("th.Standings-header-team").first();
Element winsElement = doc.select("th[title='Match Wins']").first();

// Get the index within the table header that the elements are at 
// (will be used to find the appropriate table data in the table row).
int teamNameIndex = teamNameElement.elementSiblingIndex();
int winsIndex = winsElement.elementSiblingIndex();

// Select the first table row. This contains the data we want to grab.  
Element tableRow = doc.select("tbody > tr").first();

// Use the indexes we found earlier to get the team name and wins <td> elements
Element teamNameData = tableRow.getElementsByIndexEquals(teamNameIndex).first();
Element winsData = tableRow.getElementsByIndexEquals(winsIndex).first();

// There are multiple span elements in the team name <td> so grab the one
// at index 1 which contains the name.  
Element name = teamNameData.select("span").get(1);

System.out.println("Team Name: " + name.text());
System.out.println("Wins: " + winsData.text());
相关问题