【问题标题】:Jsoup: Table with class nameJsoup:带有类名的表
【发布时间】:2018-03-13 02:26:18
【问题描述】:

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

【问题讨论】:

  • 让我们从描述您如何知道要提取哪些数据开始?你如何识别/找到它?
  • @Pshemo 我知道我需要团队名称和记录,我将把输出导入到 Excel 表中,然后在我的 Java Web 应用程序中使用该表。我看到像 IconLabel-Item 这样的跨度类存储了团队名称等信息,但我还没有抓取该跨度的属性来获取字符串。

标签: java html jsoup


【解决方案1】:

以下代码应该能够解析您要查找的数据。我在代码中添加了 cmets 以帮助您了解每个步骤在做什么。

// 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

【讨论】:

    【解决方案2】:

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

    <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,如果解析正确,它将为您提供表格中的所有信息以及更多信息

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多