getElementsByTagName
获取所有 <table>
$html = <<<HTML
<div id="out">
<span id="oddsTable"></span>
<table cellpadding="0" cellspacing="0" border="0" width="900" align="center">第一个表格...</table>
<table cellpadding="0" cellspacing="0" border="0" width="900" align="center">第二个表格...</table>
</div>
HTML;
$dom = new DOMDocument();
libxml_use_internal_errors(true); // 忽略 HTML 解析错误
$dom->loadHTML($html);
libxml_clear_errors();
// 获取所有 table 元素
$tables = $dom->getElementsByTagName('table');
foreach ($tables as $table) {
echo $dom->saveHTML($table) . "\n";
}
输出:
<table cellpadding="0" cellspacing="0" border="0" width="900" align="center">第一个表格...</table>
<table cellpadding="0" cellspacing="0" border="0" width="900" align="center">第二个表格...</table>
div#out
下的所有 <table>
如果只想获取 div#out
下的 table
,可以这样:
$dom = new DOMDocument();
$dom->loadHTML($html);
$outDiv = $dom->getElementById('out'); // 获取 id="out" 的 div
if ($outDiv) {
$tables = $outDiv->getElementsByTagName('table');
foreach ($tables as $table) {
echo $dom->saveHTML($table) . "\n";
}
}
<table>
如果 DOM 结构更复杂,可以递归遍历子节点:
function findTables(DOMNode $node) {
$tables = [];
foreach ($node->childNodes as $child) {
if ($child instanceof DOMElement) {
if ($child->tagName === 'table') {
$tables[] = $child;
}
// 递归查找子节点
$tables = array_merge($tables, findTables($child));
}
}
return $tables;
}
$dom = new DOMDocument();
$dom->loadHTML($html);
$tables = findTables($dom);
foreach ($tables as $table) {
echo $dom->saveHTML($table) . "\n";
}
方法 | 适用场景 | 优点 | 缺点 |
---|---|---|---|
getElementsByTagName |
获取所有 <table> |
简单直接 | 无法限定父级 |
getElementById + getElementsByTagName |
获取特定容器下的 <table> |
更精准 | 需要知道父级 ID |
递归遍历 | 复杂 DOM 结构 | 灵活可控 | 代码稍复杂 |
// 加载 HTML 内容
$html = '<div id="out">
<span id="oddsTable"></span>
<table cellpadding="0" cellspacing="0" border="0" width="900" align="center">第一个表格内容...</table>
<table cellpadding="0" cellspacing="0" border="0" width="900" align="center">第二个表格内容...</table>
</div>';
// 创建 DOMDocument 对象
$dom = new DOMDocument();
libxml_use_internal_errors(true); // 禁止显示因HTML不规范而产生的警告
$dom->loadHTML($html);
libxml_clear_errors();
// 创建 XPath 对象
$xpath = new DOMXPath($dom);
// 方法1: 获取所有 table 元素
$tables = $xpath->query('//table');
foreach ($tables as $table) {
echo $dom->saveHTML($table) . "\n";
}
// 方法2: 获取 div#out 下的所有 table 元素
$tables = $xpath->query('//div[@id="out"]/table');
foreach ($tables as $table) {
echo $dom->saveHTML($table) . "\n";
}
// 方法3: 获取特定属性的 table 元素
$tables = $xpath->query('//table[@width="900" and @align="center"]');
foreach ($tables as $table) {
echo $dom->saveHTML($table) . "\n";
}
加载 HTML:首先将 HTML 内容加载到 DOMDocument 对象中。
创建 XPath:使用 DOMXPath 来查询 DOM 文档。
查询方法:
//table
:获取文档中所有 table 元素
//div[@id="out"]/table
:获取 id 为 "out" 的 div 下的直接 table 子元素
//table[@width="900" and @align="center"]
:获取具有特定属性(width=900 且 align=center)的 table 元素
处理结果:使用 foreach 遍历查询结果,并使用 saveHTML 方法输出每个 table 的 HTML 内容。
如果 HTML 不规范,建议使用 libxml_use_internal_errors(true)
来抑制警告
查询后记得使用 libxml_clear_errors()
清除错误
可以根据需要调整 XPath 表达式来精确获取你需要的 table 元素