【发布时间】:2021-05-22 19:43:34
【问题描述】:
我想从以下表格中获取数据,有些我已经获取了,有些我无法获取。
<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">
‭<span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"><span class="coordinateX">(‭0‬</span><span class="coordinatePipe">|</span><span class="coordinateY">‭−‭28‬‬)</span></span>‬ </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 <span class="timer" counting="down" value="246">0:04:06</span> hrs.</div>
<div class="at"><span>at 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>
我的输出是这样的:
【问题讨论】:
-
@mickmackusa 输入了代码,但它没有让我发布问题,因为错误说“代码太多”。但是我改变了问题并输入了代码。当我问一个问题时,因为我已经研究过答案而没有任何结果,这对我来说是一个新领域,很多次看到结果已经让我理解得更快。
标签: php html dom xpath domxpath