Some time ago I asked a question about regular expressions and a user answered me using this code, using xpath
:
$dom = new DomDocument;
$dom->loadHTMLFile("http://ciagri.iea.sp.gov.br/precosdiarios/");
$xpath = new DomXPath($dom);
// essa query pega o todos os TDs na posicao 3 da primeira tabela com a classe
$nodes = $xpath->query("(//table[@class='tabela_dados'])[1]/tr/td[position()=3]");
foreach ($nodes as $i => $node) {
echo $node->nodeValue . "\n"; // vai imprimir todos os preços
}
In the xpath
documentation the description of how the nodes data is stored is not clear to my understanding.
Is there a way to access a certain nodespace without using foreach
?