I want to extract from a table in a Web page the content of this one plus the reference that each of them has, I already extract the table but I am missing how to get to the href
tag, can you help me?
This is the table:
The code used is as follows:
$tabla = $html->getElementsByTagName("table")->item(1);
foreach($tabla->getElementsByTagName('tr') as $tr) {
$tds = $tr->getElementsByTagName('td'); // get the columns in this row
$href = $tr->getElementsByTagName('a'); //->item(0)->getAttribute('href');
if (trim($tds->item(0)->nodeValue) <> '') {
echo $tds->item(0)->nodeValue." , ".
$tds->item(1)->nodeValue." , ".
$tds->item(1)->nodeValue." , ".
$tds->item(3)->nodeValue." , ".
$tds->item(4)->nodeValue." , ".
$tds->item(5)->nodeValue." , ".
$tds->item(6)->nodeValue." , ".
$tds->item(7)->nodeValue." , ".
$tds->item(8)->nodeValue." , ";
//$href;
echo "<br />";
}
}
Thanks in advance
Ulysses
Thanks to all of you for your answers, I have implemented them but it throws me error, for better clarity I have all the code, if they execute it, the error that I show below appears:
<?php
error_reporting(0);
$html = new DomDocument;
$source = file_get_contents("http://seia.sea.gob.cl/busqueda/buscarProyectoAction.php?nombre=central&_paginador_refresh=0&_paginador_fila_actual=2");
$html->loadHTML($source);
// Cada TR
$tabla = $html->getElementsByTagName("table")->item(1);
foreach($tabla->getElementsByTagName('tr') as $tr)
{
$tds = $tr->getElementsByTagName('td'); // get the columns in this row
//$href = $tds->getAttribute('href');
$href = $tds->getElementsByTagName('a')->item(0)->getAttribute('href');
if (trim($tds->item(0)->nodeValue) <> '') {
echo $tds->item(0)->nodeValue." , ".
$tds->item(1)->nodeValue." , ".
$tds->item(1)->nodeValue." , ".
$tds->item(3)->nodeValue." , ".
$tds->item(4)->nodeValue." , ".
$tds->item(5)->nodeValue." , ".
$tds->item(6)->nodeValue." , ".
$tds->item(7)->nodeValue." , ".
$tds->item(8)->nodeValue." , ";
//$href;
echo "<br />";
}
//break; // don't check any further rows
}
?>