簡體   English   中英

Jsoup:具有類名的表

[英]Jsoup: Table with class name

我最近開始使用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>

經過一番挖掘后,似乎有一個腳本加載了表,這是我的猜測

<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=zh_CN它將為您提供表中的所有信息以及更多信息

以下代碼應能夠解析您要查找的數據。 我在代碼中添加了注釋,以幫助您了解每個步驟在做什么。

// 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());

輸出:

Team Name: New York Excelsior
Wins: 14

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM