从包含特定 tbody 标签中的特定类的多个表格单元格中抓取值

问题描述

我想从下表中获取数据,有些我已经获取了,有些我无法获取

<table cellspacing="1" cellpadding="1" class="troop_details inAttack"
            >
                <thead>
                    <tr>
                        <td class="role">
                                                    <a href="/karte.PHP?d=91629">02]</a>
                                            </td>
                        <td colspan="11" class="troopHeadline">
                                                    <a class="markAttack" onclick="Travian.AttackSymbol.markAttackSymbol(29447487);return false;"><img id="markSymbol_29447487" class="markAttack markAttack0" src="/img/x.gif" title="mark attack" alt="mark attack" /></a>
                                                                        <a href="/karte.PHP?d=91628">Uanm attacks 01] #WorkInProgress</a>
                                            </td>
                    </tr>
                </thead>
                <tbody class="units">
                    <tr>
                        <th class="coords">
                                                    &#x202d;<span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"><span class="coordinateX">(&#x202d;0&#x202c;</span><span class="coordinatePipe">|</span><span class="coordinateY">&#x202d;&minus;&#x202d;28&#x202c;&#x202c;)</span></span>&#x202c;                                   </th>
                                            <td class="uniticon">
                                <img class="unit u21" title="Phalanx: 0:04:17" alt="Phalanx" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u22" title="Swordsman: 0:05:00" alt="Swordsman" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u23" title="pathfinder: 0:01:46" alt="pathfinder" src="/img/x.gif" />                  </td>
                                            <td class="uniticon">
                                <img class="unit u24" title="Theutates Thunder: 0:01:35" alt="Theutates Thunder" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u25" title="Druidrider: 0:01:53" alt="Druidrider" src="/img/x.gif" />                  </td>
                                            <td class="uniticon">
                                <img class="unit u26" title="Haeduan: 0:02:18" alt="Haeduan" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u27" title="Ram: 0:07:30" alt="Ram" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u28" title="Trebuchet: 0:10:00" alt="Trebuchet" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u29" title="Chieftain: 0:06:00" alt="Chieftain" src="/img/x.gif" />                    </td>
                                            <td class="uniticon">
                                <img class="unit u30" title="Settler: 0:06:00" alt="Settler" src="/img/x.gif" />                    </td>
                                                            <td class="uniticon last">
                                <img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" />                 </td>
                                    </tr>
                </tbody>
        
                <tbody class="units last">
                    <tr>
                        <th>Troops</th>
                                                            <td class="unit">
                                                            1                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none">
                                                            0                                           </td>
                                            <td class="unit none last">
                                                            0                                           </td>
                                    </tr>
                </tbody>
        
                
                <tbody class="infos">
                    <tr>
                        <th>Arrival</th>
                        <td colspan="11">
                            <div class="in">in&nbsp;<span  class="timer" counting="down" value="246">0:04:06</span>&nbsp;hrs.</div>
                            <div class="at"><span>at&nbsp;14:23:09</span><span> </span></div>
                        </td>
                    </tr>
                </tbody>
            </table>

我对坐标和单位特别感兴趣:

0
-28
1 0 0 0 0 0 0 0 0 0 0

为了获取 html 的其他部分,我使用了 DOMXPath 类,但在这种情况下我无法访问该数据。

编辑:这是我目前的代码,我能够获取一些数据。

如何删除 y 坐标的最后一个字符?即括号“)”。

我怎样才能得到单位值?即 1 0 0 0 0 0 0 等

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($CasermaUtente);
$xpath = new DOMXPath($dom);
$texts = [];
$count = 0;
foreach ($xpath->query("//table[contains(@class,'troop_details') and contains(@class,'inAttack')]") as $table) {
  $tablePath = $table->getNodePath();
   $texts[] = [
        $xpath->query($tablePath . "//td[@class='troopHeadline']//a[@href]/text()")[0]->nodeValue
  ];
  $orario[] = [
        substr($xpath->query($tablePath . "//div[@class='at']/span[starts-with(text(),'at')]/text()")[0]->nodeValue,-8)
  ];
  $xcoord[] = [
        mb_substr($xpath->query($tablePath . "//span[@class='coordinateX']/text()")[0]->nodeValue,1)
  ];
  $ycoord[] = [
        $xpath->query($tablePath . "//span[@class='coordinateY']/text()")[0]->nodeValue
  ];
$count = $count + 1;
}

?>

<table width="350px" border="1">
<?PHP
for ($i=0; $i<$count ; $i++) { ?>
<tr><td align="center"> <?PHP $stampa_xcoord = implode($xcoord[$i]);
echo $stampa_xcoord; ?>
</td>
<td align="center"> <?PHP $stampa_ycoord = implode($ycoord[$i]);
echo $stampa_ycoord; ?>
<td align="center"><?PHP $stampa_stringa = implode($texts[$i]);
echo $stampa_stringa; ?>
</td>
<td align="center"> <?PHP $stampa_orario = implode($orario[$i]);
echo $stampa_orario; }?>
</table>

我的输出是这样的:

enter image description here

解决方法

您需要创建一个内部循环来获取所有 unit 值。

strstr() 的第三个参数为 true 是一种很好的技术,当您想在另一个子字符串第一次出现之前隔离子字符串时,可以使用它。如果保证 ] 存在于 text 字符串中,则这是可靠的。如果不能保证该符号存在,那么 explode() 可能更合适(在这种情况下,您可以无条件地访问爆炸创建的数组的第一个元素)。

我喜欢 printf() 语法的简洁性,而不是用 php 变量来回应大量的 html 标记。

代码:(Demo)

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($CasermaUtente);
$xpath = new DOMXPath($dom);
$texts = [];
$orario = [];
$xcoord = [];
$ycoord = [];
$unita = [];
foreach ($xpath->query("//table[contains(@class,'troop_details') and contains(@class,'inAttack')]") as $i => $table) {
    $tablePath = $table->getNodePath();
    $texts[] = strstr($xpath->query("$tablePath//td[@class='troopHeadline']//a[@href]/text()")[0]->nodeValue,']',true);
    $orario[] = substr($xpath->query("$tablePath//div[@class='at']/span[starts-with(text(),'at')]/text()")[0]->nodeValue,-8);
    $xcoord[] = ltrim($xpath->query("$tablePath//span[@class='coordinateX']")[0]->nodeValue,'(');
    $ycoord[] = rtrim($xpath->query("$tablePath//span[@class='coordinateY']")[0]->nodeValue,')');
    foreach ($xpath->query("$tablePath//tbody[contains(@class,'units') and contains(@class,'last')]//td[contains(@class,'unit')]/text()") as $unit) {
        $unita[] = trim($unit->nodeValue);
    }
}

echo '<table width="350px" border="1">';
foreach ($texts as $i => $text) {
    printf('
        <tr>
            <td align="center">%s</td>
            <td align="center">%s</td>
            <td align="center">%s</td>
            <td align="center">%s</td>
            <td align="center">%s</td>
        </tr>',$xcoord[$i],$ycoord[$i],$text,$orario[$i],implode(' ',$unita)
    );
    echo "\n"; // for readability in online demo
}
echo '</table>';

输出:

<table width="350px" border="1">
        <tr>
            <td align="center">‭0‬</td>
            <td align="center">‭−‭28‬‬</td>
            <td align="center">Uanm attacks 01</td>
            <td align="center">14:23:09</td>
            <td align="center">1 0 0 0 0 0 0 0 0 0 0</td>
        </tr>
</table>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...