Php 通过it定位TD位置';s TH,domCrawler

Php 通过it定位TD位置';s TH,domCrawler,php,web-scraping,domcrawler,Php,Web Scraping,Domcrawler,我试图刮去表的td标签,但首先我需要检查th。例如,假设表结构如下所示 <tbody> <tr> <th>color</th> <td>red</td> </tr> <tr> <th>price</th> <td>23.267$</td> </tr> <tr>

我试图刮去
表的
td
标签,但首先我需要检查
th
。例如,假设表结构如下所示

<tbody>
  <tr>
      <th>color</th>
      <td>red</td>
  </tr>
  <tr>
      <th>price</th>
      <td>23.267$</td>
  </tr>
  <tr>
      <th>brand</th>
      <td>mustang</td>
  </tr>
</tbody>

颜色
红色
价格
23.267$
品牌
野马
在这个表中,我需要刮取
mustang
值。但是我不能使用
$crawler->filter('table td')->eq(3)。因为位置总是在变化。所以我需要通过它的
th
来捕捉值。我的意思是,如果
th
的值是brand,那么就得到它的
td


最好的方法是什么

不确定这是不是最好的解决方案,但我用以下方法解决了它:

$props = $node->filter("table th")->each(function($th, $i){
    return $th->text();
});
$vals = $node->filter("table td")->each(function($td, $i){
    return $td->text();
});

$items = [
    "brand" => "", 
    "color" => "",
];

for ($a=0; $a < count($props); $a++) { 
    switch ($props[$a]) {
        case 'brand':
            $items["brand"] = $vals[$a];
            break;                        
    }
}
$props=$node->filter(“table th”)->每个函数($th,$i){
返回$th->text();
});
$VAL=$node->filter(“table td”)->每个函数($td,$i){
返回$td->text();
});
$items=[
“品牌”=>“,
“颜色”=>“”,
];
对于($a=0;$a

如果有其他方法或更好的方法来实现这一点。请随意张贴在这里。多谢各位

到目前为止你试过什么?我把它贴出来作为答案。请检查一下@blueenvelopeDoes domcrawler支持:包含?如果是这样,它是
th:contains(“brand”)+td