根据 <th> 的内部文本用 Simple HTML Dom 抓取 <tr>,但 <th> 没有属性

Grabbing <tr> with Simple HTML Dom based on inner text of <th>, but <th> has no attributes

我有一个看起来像这样的 HTML,我正在尝试使用简单的 HTML DOM 来获取名称值和昵称值。

<tr>.....</tr>
<tr>.....</tr>
<tr>
<th>Name</th>
<td>John</td>
</tr>
<tr>
<th><span>Nickname</span></th>
<td>Johny</td>
</tr>
<tr>.....</tr>
<tr>.....</tr>

我遇到了困难,因为 th 和 td 没有 id 或 类

所以我尝试将 innertext 值与以下 php 代码一起使用

require_once('lib/simple_html_dom.php');
$url='http://123.com';
$html = file_get_html($url);

foreach ( $html->find ( 'tr th' ) as $element ) {

if ($element->innertext = 'Name'){

    $element = $element->parent;

    $tinfo = $element->find ( 'td', 0 );

    $info = $tinfo->plaintext;

    echo $info;}
}

$html = file_get_html($url);
foreach ( $html->find ( 'tr th span' ) as $element ) {

if ($element->innertext = 'Nickname'){

    $element = $element->parent;

    $tinfo = $element->find ( 'td', 0 );

    $info = $tinfo->plaintext;

  echo $info;}
}

我似乎无法让上面的代码工作,对此有什么建议吗?我该怎么做?

删除一些冲突的脚本后,我的新代码,按照 Ghost 的建议,现在可以用于抓取姓名和昵称值

<?php
$name = $nickname ='';
foreach($html->find('tr') as $tr) { // each row
    foreach($tr->childNodes() as $tdh) { // each cell of that row
        if($tdh->tag == 'th' && $tdh->innertext == 'Name') {
            $name = $tdh->next_sibling()->innertext;
        }
    }
}

echo $name;
foreach($html->find('tr th') as $tr) { // each row
    foreach($tr->childNodes() as $tdh) { // each cell of that row
        if($tdh->tag == 'span' && $tdh->innertext == 'Nickname') {
            $nickname = $tdh->parent->next_sibling()->innertext;
        }
    }
}

echo $nickname;
?>

答案归功于 Ghost。