Domxpath and foreach. How to have a preview of the captured elements?

Asked

Viewed 55 times

0

I’m learning to deal with DOMXpath in the php. Was using regex (but I was discouraged here in the stack when for html capture). I confess that for me it is not so simple and the DOM has its limits (when there are spaces in tag names and also in bug management). If anyone can help me with the command on php to get a preview of the captured elements and check that everything is all right, I would appreciate it. If you have suggestions to improve the code, you are also welcome.

<?php
    $doc = new DOMDocument;
    libxml_use_internal_errors(true);
    // Eliminando espaços em branco (caso existam)
    $doc->preserveWhiteSpace = false;
    @$doc->loadHTML(file_get_contents ('http://www.imdb.com/search/title?certificates=us:pg_13&genres=comedy&groups=top_250'));
    $xpath = new DOMXPath($doc);
    // Iniciando a partir do elemento raiz
    $grupos = $xpath->query(".//*[@class='lister-item mode-advanced']");
    // Criando um array e depois um loop com os elementos a serem capturados (imagem, titulo e link)
    $resultados = array();
    foreach($grupos as $grupo) {
        $i = $xpath->query(".//*[@class='loadlate']//@src", $grupo);
        $t = $xpath->query(".//*[@class='lister-item-header']//a/text()", $grupo);
        $l = $xpath->query(".//*[@class='lister-item-header']//a/@href", $grupo);

    $resultados[] = $resultado;

}
// Que comando deveria usar para ter uma prévia dos resultados e verificar se está tudo ok?
print_r($resultado);
  • I never used that XPath, but from what I see, you haven’t declared the variable $resultado and I think you wanted to show the $resultados, not the $resultado.

  • @Francis. I declare here $resultados[] = $resultado;

  • No, you didn’t declare.

  • The question was answered on: https://stackoverflow.com/questions/46759935/domxpath-and-foreach-how-to-get-a-preview-of-the-captured-elements/46760971#46760971

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.