问题:在我的代码中我想删除标题tr html标记(女巫属性为<tr class='background1'>
)
urL http://www.rayansaba.com/index.php?ukey=pricelist
在下面的代码中,如果条件要避免标题,但我不能删除。
$table_rows = $xpath->query("//div[@class='cpt_maincontent']/center/table/tr"); // target the row (the browser rendered <tbody>, but actually it really doesnt have one)
if($table_rows->length <= 0) { // exit if not found
echo 'no table rows found';
exit;
}
$i = 0;
$trlength = count($table_rows);
foreach($table_rows as $tr){
$row = $tr->childNodes;
if($row->item(0)->tagName!='<tr class="background1"></tr>') { // avoid headers
$data[] = array(
'Name' => trim($row->item(0)->nodeValue),
'Price' => trim($row->item(2)->nodeValue),
);
}
}
答案 0 :(得分:0)
实际上,您可以在xpath查询中添加它,排除那些带背景的行,然后提取值并相应地推送节点值。例如:
$dom = new DOMDocument;
@$dom->loadHTMLFile('http://www.rayansaba.com/index.php?ukey=pricelist');
$xpath = new DOMXpath($dom);
$table_rows = $xpath->query("//div[@class='cpt_maincontent']/center/table/tr[not(@class)]");
$data = array();
foreach($table_rows as $tr) {
if($tr->getElementsByTagName('td')->length > 2) {
$name = trim($tr->getElementsByTagName('td')->item(0)->nodeValue);
$price = trim($tr->getElementsByTagName('td')->item(2)->nodeValue);
$data[] = array('Name' => $name, 'Price' => $price);
}
}
echo '<pre>', print_r($data, 1), '</pre>';