从 html 文档中的多个 <table> 标签中抓取相关文本对
Scrape related pairs of text from multiple <table> tags in an html document
我在数据库字段中有这个 html 源代码。我想分析这段代码,特别是一些表的字段,并将它们打印在屏幕上。我无法发布所有代码,因为它超过 3000 行代码,这是代码的开头:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="mainLayout">
<head>
<title>Anglosphere x3</title>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="content-language" content="en-US" />
<meta name="viewport" content="width=device-width">
<script data-cmp-ab="1">window.cmp_block_ignoredomains = ['recaptcha.net']</script>
<script data-cmp-ab="1" src="https://cdn.consentmanager.mgr.consensu.org/delivery/cookieblock.min.js" ></script>
<link rel="stylesheet" href="https://cdn.consentmanager.mgr.consensu.org/delivery/cmp.min.css" />
这是关于我感兴趣的表格的代码:
<table cellspacing="1" cellpadding="1" class="troop_details inReturn"
>
<thead>
<tr>
<td class="role">
<a href="/karte.php?d=91628">01] #WorkInProgress</a>
</td>
<td colspan="11" class="troopHeadline">
<a href="/karte.php?d=91611">Return from 01-soldier</a>
</td>
</tr>
</thead>
<tbody class="units">
<tr>
<th class="coords">
‭<span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"><span class="coordinateX">(‭−‭1‬‬</span><span class="coordinatePipe">|</span><span class="coordinateY">‭−‭28‬‬)</span></span>‬ </th>
<td class="uniticon">
<img class="unit u21" title="Phalanx: 1:12:51" alt="Phalanx" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u22" title="Swordsman: 1:25:00" alt="Swordsman" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u23" title="Pathfinder: 0:30:00" alt="Pathfinder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u24" title="Theutates Thunder: 0:26:51" alt="Theutates Thunder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u25" title="Druidrider: 0:31:53" alt="Druidrider" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u26" title="Haeduan: 0:39:14" alt="Haeduan" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u27" title="Ram: 2:07:30" alt="Ram" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u28" title="Trebuchet: 2:50:00" alt="Trebuchet" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u29" title="Chieftain: 1:42:00" alt="Chieftain" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u30" title="Settler: 1:42: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 none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit">
500 </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>Bounty</th>
<td colspan="11">
<div class="res">
<div class="inlineIconList resourceWrapper"><div class="inlineIcon resources" title="Lumber"><i class="r1"></i><span class="value ">6758</span></div><div class="inlineIcon resources" title="Clay"><i class="r2"></i><span class="value ">8093</span></div><div class="inlineIcon resources" title="Iron"><i class="r3"></i><span class="value ">6908</span></div><div class="inlineIcon resources" title="Crop"><i class="r4"></i><span class="value ">15741</span></div></div> </div>
<div class="carry">
<img class="carry full" title="carry"
alt="carry"
src="/img/x.gif"/> ‭‭37500‬ / ‭37500‬‬ </div>
</td>
</tr>
</tbody>
<tbody class="infos">
<tr>
<th>Arrival</th>
<td colspan="11">
<div class="in">in <span class="timer" counting="down" value="85">0:01:25</span> hrs.</div>
<div class="at"><span>at 00:43:10</span><span> </span></div>
</td>
</tr>
</tbody>
</table>
<a name="at"></a>
<table cellspacing="1" cellpadding="1" class="troop_details inReturn"
>
<thead>
<tr>
<td class="role">
<a href="/karte.php?d=91628">01] #WorkInProgress</a>
</td>
<td colspan="11" class="troopHeadline">
<a href="/karte.php?d=94829">Return from 0-New Hulk</a>
</td>
</tr>
</thead>
<tbody class="units">
<tr>
<th class="coords">
‭<span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"><span class="coordinateX">(‭−‭1‬‬</span><span class="coordinatePipe">|</span><span class="coordinateY">‭−‭28‬‬)</span></span>‬ </th>
<td class="uniticon">
<img class="unit u21" title="Phalanx: 0:45:33" alt="Phalanx" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u22" title="Swordsman: 0:53:09" alt="Swordsman" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u23" title="Pathfinder: 0:18:46" alt="Pathfinder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u24" title="Theutates Thunder: 0:16:47" alt="Theutates Thunder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u25" title="Druidrider: 0:19:56" alt="Druidrider" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u26" title="Haeduan: 0:24:32" alt="Haeduan" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u27" title="Ram: 1:19:44" alt="Ram" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u28" title="Trebuchet: 1:46:18" alt="Trebuchet" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u29" title="Chieftain: 1:03:47" alt="Chieftain" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u30" title="Settler: 1:03:47" 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 none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit">
400 </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>Bounty</th>
<td colspan="11">
<div class="res">
<div class="inlineIconList resourceWrapper"><div class="inlineIcon resources" title="Lumber"><i class="r1"></i><span class="value ">6130</span></div><div class="inlineIcon resources" title="Clay"><i class="r2"></i><span class="value ">5835</span></div><div class="inlineIcon resources" title="Iron"><i class="r3"></i><span class="value ">5638</span></div><div class="inlineIcon resources" title="Crop"><i class="r4"></i><span class="value ">12397</span></div></div> </div>
<div class="carry">
<img class="carry full" title="carry"
alt="carry"
src="/img/x.gif"/> ‭‭30000‬ / ‭30000‬‬ </div>
</td>
</tr>
</tbody>
<tbody class="infos">
<tr>
<th>Arrival</th>
<td colspan="11">
<div class="in">in <span class="timer" counting="down" value="920">0:15:20</span> hrs.</div>
<div class="at"><span>at 00:57:05</span><span> </span></div>
</td>
</tr>
</tbody>
</table>
我感兴趣的数据如下:
- Return 来自 01-士兵 00:43:10
- Return 来自 0-新绿巨人 00:57:05
这是我的代码,但它给出了一个空的 array ( );
<?php
include_once('simple_html_dom.php');
$caserma = $_SESSION["caserma"];
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($_SESSION["caserma"]);
$xpath = new DOMXPath($dom);
$texts = [];
foreach ($xpath->query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]//td[@class='troopHeadline']//a[@href]/text()") as $textNode) {
$texts[] = $textNode->nodeValue;
}
var_export($texts);
?>
我认为我的输入无效 xml/html,所以我尝试寻找这样的错误:
$object = simplexml_load_string($_SESSION["caserma"]);
if ($object === false) {
$errors = libxml_get_errors();
print_r($errors)
}
这是我的输出:
Array ( [0] => LibXMLError Object ( [level] => 3 [code] => 4 [column]
=> 1 [message] => Start tag expected, '<' not found [file] => [line] => 1 ) )
我该如何解决?
您似乎想要访问 <div class="at">
文本,该文本与先前匹配的文本存在于同一 <table>
中。
通常,我只是将 $table
变量提供给嵌套的 xpath query()
调用,但我认为你有太多畸形的 html 不可靠(这是我的在测试时发现,除非我只是在我的演示中搞砸了)。
另一种可行的技术是获取节点路径并将其添加到嵌套的 xpath 路径字符串中。参见:
代码:(Demo)
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($_SESSION["caserma"]);
$xpath = new DOMXPath($dom);
$texts = [];
foreach ($xpath->query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]") as $table) {
$tablePath = $table->getNodePath();
$texts[] = [
'troopHeadline' => $xpath->query($tablePath . "//td[@class='troopHeadline']//a[@href]/text()")[0]->nodeValue,
'atTime' => substr($xpath->query($tablePath . "//div[@class='at']/span[starts-with(text(), 'at')]/text()")[0]->nodeValue, -8)
];
}
var_export($texts);
输出:
array (
0 =>
array (
'troopHeadline' => 'Return from 01-soldier',
'atTime' => '00:43:10',
),
1 =>
array (
'troopHeadline' => 'Return from 0-New Hulk',
'atTime' => '00:57:05',
),
)
我在数据库字段中有这个 html 源代码。我想分析这段代码,特别是一些表的字段,并将它们打印在屏幕上。我无法发布所有代码,因为它超过 3000 行代码,这是代码的开头:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="mainLayout">
<head>
<title>Anglosphere x3</title>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="content-language" content="en-US" />
<meta name="viewport" content="width=device-width">
<script data-cmp-ab="1">window.cmp_block_ignoredomains = ['recaptcha.net']</script>
<script data-cmp-ab="1" src="https://cdn.consentmanager.mgr.consensu.org/delivery/cookieblock.min.js" ></script>
<link rel="stylesheet" href="https://cdn.consentmanager.mgr.consensu.org/delivery/cmp.min.css" />
这是关于我感兴趣的表格的代码:
<table cellspacing="1" cellpadding="1" class="troop_details inReturn"
>
<thead>
<tr>
<td class="role">
<a href="/karte.php?d=91628">01] #WorkInProgress</a>
</td>
<td colspan="11" class="troopHeadline">
<a href="/karte.php?d=91611">Return from 01-soldier</a>
</td>
</tr>
</thead>
<tbody class="units">
<tr>
<th class="coords">
‭<span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"><span class="coordinateX">(‭−‭1‬‬</span><span class="coordinatePipe">|</span><span class="coordinateY">‭−‭28‬‬)</span></span>‬ </th>
<td class="uniticon">
<img class="unit u21" title="Phalanx: 1:12:51" alt="Phalanx" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u22" title="Swordsman: 1:25:00" alt="Swordsman" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u23" title="Pathfinder: 0:30:00" alt="Pathfinder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u24" title="Theutates Thunder: 0:26:51" alt="Theutates Thunder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u25" title="Druidrider: 0:31:53" alt="Druidrider" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u26" title="Haeduan: 0:39:14" alt="Haeduan" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u27" title="Ram: 2:07:30" alt="Ram" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u28" title="Trebuchet: 2:50:00" alt="Trebuchet" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u29" title="Chieftain: 1:42:00" alt="Chieftain" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u30" title="Settler: 1:42: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 none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit">
500 </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>Bounty</th>
<td colspan="11">
<div class="res">
<div class="inlineIconList resourceWrapper"><div class="inlineIcon resources" title="Lumber"><i class="r1"></i><span class="value ">6758</span></div><div class="inlineIcon resources" title="Clay"><i class="r2"></i><span class="value ">8093</span></div><div class="inlineIcon resources" title="Iron"><i class="r3"></i><span class="value ">6908</span></div><div class="inlineIcon resources" title="Crop"><i class="r4"></i><span class="value ">15741</span></div></div> </div>
<div class="carry">
<img class="carry full" title="carry"
alt="carry"
src="/img/x.gif"/> ‭‭37500‬ / ‭37500‬‬ </div>
</td>
</tr>
</tbody>
<tbody class="infos">
<tr>
<th>Arrival</th>
<td colspan="11">
<div class="in">in <span class="timer" counting="down" value="85">0:01:25</span> hrs.</div>
<div class="at"><span>at 00:43:10</span><span> </span></div>
</td>
</tr>
</tbody>
</table>
<a name="at"></a>
<table cellspacing="1" cellpadding="1" class="troop_details inReturn"
>
<thead>
<tr>
<td class="role">
<a href="/karte.php?d=91628">01] #WorkInProgress</a>
</td>
<td colspan="11" class="troopHeadline">
<a href="/karte.php?d=94829">Return from 0-New Hulk</a>
</td>
</tr>
</thead>
<tbody class="units">
<tr>
<th class="coords">
‭<span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"><span class="coordinateX">(‭−‭1‬‬</span><span class="coordinatePipe">|</span><span class="coordinateY">‭−‭28‬‬)</span></span>‬ </th>
<td class="uniticon">
<img class="unit u21" title="Phalanx: 0:45:33" alt="Phalanx" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u22" title="Swordsman: 0:53:09" alt="Swordsman" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u23" title="Pathfinder: 0:18:46" alt="Pathfinder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u24" title="Theutates Thunder: 0:16:47" alt="Theutates Thunder" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u25" title="Druidrider: 0:19:56" alt="Druidrider" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u26" title="Haeduan: 0:24:32" alt="Haeduan" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u27" title="Ram: 1:19:44" alt="Ram" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u28" title="Trebuchet: 1:46:18" alt="Trebuchet" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u29" title="Chieftain: 1:03:47" alt="Chieftain" src="/img/x.gif" /> </td>
<td class="uniticon">
<img class="unit u30" title="Settler: 1:03:47" 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 none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit none">
0 </td>
<td class="unit">
400 </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>Bounty</th>
<td colspan="11">
<div class="res">
<div class="inlineIconList resourceWrapper"><div class="inlineIcon resources" title="Lumber"><i class="r1"></i><span class="value ">6130</span></div><div class="inlineIcon resources" title="Clay"><i class="r2"></i><span class="value ">5835</span></div><div class="inlineIcon resources" title="Iron"><i class="r3"></i><span class="value ">5638</span></div><div class="inlineIcon resources" title="Crop"><i class="r4"></i><span class="value ">12397</span></div></div> </div>
<div class="carry">
<img class="carry full" title="carry"
alt="carry"
src="/img/x.gif"/> ‭‭30000‬ / ‭30000‬‬ </div>
</td>
</tr>
</tbody>
<tbody class="infos">
<tr>
<th>Arrival</th>
<td colspan="11">
<div class="in">in <span class="timer" counting="down" value="920">0:15:20</span> hrs.</div>
<div class="at"><span>at 00:57:05</span><span> </span></div>
</td>
</tr>
</tbody>
</table>
我感兴趣的数据如下:
- Return 来自 01-士兵 00:43:10
- Return 来自 0-新绿巨人 00:57:05
这是我的代码,但它给出了一个空的 array ( );
<?php
include_once('simple_html_dom.php');
$caserma = $_SESSION["caserma"];
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($_SESSION["caserma"]);
$xpath = new DOMXPath($dom);
$texts = [];
foreach ($xpath->query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]//td[@class='troopHeadline']//a[@href]/text()") as $textNode) {
$texts[] = $textNode->nodeValue;
}
var_export($texts);
?>
我认为我的输入无效 xml/html,所以我尝试寻找这样的错误:
$object = simplexml_load_string($_SESSION["caserma"]);
if ($object === false) {
$errors = libxml_get_errors();
print_r($errors)
}
这是我的输出:
Array ( [0] => LibXMLError Object ( [level] => 3 [code] => 4 [column] => 1 [message] => Start tag expected, '<' not found [file] => [line] => 1 ) )
我该如何解决?
您似乎想要访问 <div class="at">
文本,该文本与先前匹配的文本存在于同一 <table>
中。
通常,我只是将 $table
变量提供给嵌套的 xpath query()
调用,但我认为你有太多畸形的 html 不可靠(这是我的在测试时发现,除非我只是在我的演示中搞砸了)。
另一种可行的技术是获取节点路径并将其添加到嵌套的 xpath 路径字符串中。参见:
代码:(Demo)
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($_SESSION["caserma"]);
$xpath = new DOMXPath($dom);
$texts = [];
foreach ($xpath->query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]") as $table) {
$tablePath = $table->getNodePath();
$texts[] = [
'troopHeadline' => $xpath->query($tablePath . "//td[@class='troopHeadline']//a[@href]/text()")[0]->nodeValue,
'atTime' => substr($xpath->query($tablePath . "//div[@class='at']/span[starts-with(text(), 'at')]/text()")[0]->nodeValue, -8)
];
}
var_export($texts);
输出:
array (
0 =>
array (
'troopHeadline' => 'Return from 01-soldier',
'atTime' => '00:43:10',
),
1 =>
array (
'troopHeadline' => 'Return from 0-New Hulk',
'atTime' => '00:57:05',
),
)