Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
XPath no funciona como se esperaba [php]

A menudo uso XPath con php para analizar páginas, pero esta vez no entiendo el comportamiento con esta página específica con el siguiente código, espero que me puedan ayudar con esto.

Código que utilizo para analizar esta página http://www.jeuxvideo.com/recherche.php?m=9&t=10&q=Call+of+duty :

 <?php $What = 'Call of duty'; $What = urlencode($What); $Query = 'http://www.jeuxvideo.com/recherche.php?m=9&t=10&q='.$What; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Query); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); $response = curl_exec($ch); curl_close($ch); /* $search = array("<article", "</article>"); $replace = array("<div", "</div>"); $response = str_replace($search, $replace, $response); */ $dom = new DOMDocument(); @$dom->loadHTML($response); $xpath = new DOMXPath($dom); $elements = $xpath->query('//article[@class="recherche-aphabetique-item"]/a'); //$elements = $xpath->query('//div[@class="recherche-aphabetique-item"]/a'); count($elements); var_dump($elements); ?>

violín para probarlo: http://phpfiddle.org/main/code/r9n6-d0j0

Solo quiero obtener todos los nodos "a" que están en los nodos "artículo" con la clase "recherche-aphabetique-item".

Pero no me devuelve nada :/.

Como puede ver en el código comentado, intenté reemplazar los artículos de elementos html5 a div, pero obtuve el mismo comportamiento.

Gracias cuatro por tu ayuda.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Estoy viendo un montón de DOMDocument::loadHTML(): Unexpected end tag debería usar las funciones internas de manejo de errores de libxml para ayudar a solucionar esto quizás. Además, cuando miré el DOM del sitio remoto, no pude ver a etiqueta que coincidiera con la consulta XPath, solo etiquetas span

 <?php $What = 'Call of duty'; $What = urlencode($What); $Query = 'http://www.jeuxvideo.com/recherche.php?m=9&t=10&q='.$What; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Query); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); $response = curl_exec($ch); curl_close($ch); /* try to suppress errors using libxml */ libxml_use_internal_errors( true ); $dom = new DOMDocument(); /* additional flags for DOMDocument */ $dom->validateOnParse=false; $dom->standalone=true; $dom->strictErrorChecking=false; $dom->recover=true; $dom->formatOutput=false; @$dom->loadHTML($response); libxml_clear_errors(); $xpath = new DOMXPath($dom); $elements = $xpath->query('//article[@class="recherche-aphabetique-item"]/span'); count( $elements ); var_dump( $elements ); ?>

producción

 object(DOMNodeList)#97 (1) { ["length"]=> int(94) }

Podría simplificar aún más esto quizás intentando:

 $What = 'Call of duty'; $What = urlencode($What); $Query = 'http://www.jeuxvideo.com/recherche.php?m=9&t=10&q='.$What; libxml_use_internal_errors( true ); $dom = new DOMDocument(); $dom->validateOnParse=false; $dom->standalone=true; $dom->strictErrorChecking=false; $dom->recover=true; $dom->formatOutput=false; @$dom->loadHTMLFile($Query); libxml_clear_errors(); $xpath = new DOMXPath($dom); $elements = $xpath->query('//article[@class="recherche-aphabetique-item"]/span'); count($elements); foreach( $elements as $node )echo $node->nodeValue,'<br />';
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!