繁体   English   中英

使用PHP解析html并循环遍历表行和列?

[英]Parse html using PHP and loop through table rows and columns?

我正在尝试从loadHTML解析HTML但是我遇到了麻烦,我设法循环遍历文档中的所有<tr>但我不知道如何遍历每行上的<td>

这是我到目前为止所做的:

$DOM->loadHTML($url);
$rows= $DOM->getElementsByTagName('tr');

for ($i = 0; $i < $rows->length; $i++) { // loop through rows
    // loop through columns
    ...
}

如何遍历每行中的列?

DOMElement还支持getElementsByTagName

$DOM = new DOMDocument();
$DOM->loadHTMLFile("file path or url");
$rows = $DOM->getElementsByTagName("tr");
for ($i = 0; $i < $rows->length; $i++) {
    $cols = $rows->item($i)->getElementsbyTagName("td");
    for ($j = 0; $j < $cols->length; $j++) {
        echo $cols->item($j)->nodeValue, "\t";
        // you can also use DOMElement::textContent
        // echo $cols->item($j)->textContent, "\t";
    }
    echo "\n";
}

会重新循环工作吗?

$DOM->loadHTML($url);
$rows= $DOM->getElementsByTagName('tr');
$tds= $DOM->getElementsByTagName('td');

for ($i = 0; $i < $rows->length; $i++) {
// loop through columns
     for ($i = 0; $i < $tds->length; $i++) {
     // loop through rows

     }

}

编辑您还必须检查parent node ,以确保该rows母公司是tr你目前在他,像这样

if ($rows == tds->parent_node){
// do whatever
}

可能在语法上不是100%正确,但概念是合理的。

使用DOMXPath通过相对xpath查询查询子列节点,如下所示:

$xpath = new DOMXPath( $DOM);
$rows= $xpath->query('//table/tr');

foreach( $rows as $row) {
    $cols = $xpath->query( 'td', $row); // Get the <td> elements that are children of this <tr>
    foreach( $cols as $col) {
        echo $col->textContent;
    }
}

编辑:要从特定行开始并停止,请通过更改迭代DOMNodeList的方式在行上保留自己的索引:

$xpath = new DOMXPath( $DOM);
$rows= $xpath->query('//table/tr');

for( $i = 3, $max = $rows->length - 2; $i < $max, $i++) {
    $row = $rows->item( $i);
    $cols = $xpath->query( 'td', $row);
    foreach( $cols as $col) {
        echo $col->textContent;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM