2016-07-13 2 views
0

Code ist unten. Ich möchte die Tabellen mit dem ID-Ergebnis der verknüpften URL nach file_get_contents analysieren. Ist das der richtige Weg? Ich bin neu in PHP, HTML und Javascript, also bitte erklären Sie auch die einfachsten Schritte. Was für mich schwer ist, ist HTML, PHP und Js zu kombinieren und es ohne Fehler zu arbeiten.Objekt HTMLCollection Fehler in meinem ersten Code

Als ich dies als eine PHP-Datei speichern und öffnen, heißt es nur [Objekt HTMLCollection]. Warum? Was ist das Problem hier?

<!DOCTYPE html> 
<html> 

<head> 
</head> 

<body> 

<?php 
$var = file_get_contents("link here"); 
// echo $var; 
?> 

<script> 
var x = document.getElementsByClassName("result-set"); 
document.write(x) 
</script> 

</body> 

</html> 
+0

Sie sollten 'DOMDocument' auf der' $ var' verwenden und das Dokument bearbeiten, wie Sie benötigen ~ 'DOMXPath' auch nützlich sein könnten – RamRaider

Antwort

0

In Ihrem Code Sie Javascript verwenden eine Sammlung von Knoten zurückzukehren, weshalb Sie die Meldung „htmlCollection“ bekommen, sondern weil die HTML vom entfernten Standort nicht in Ihrer HTML-Seite ist, wird es eine leer sein Sammlung sowieso. Sie können die Methode verwenden, die Sie gezeigt haben, aber Sie müssten die Antwort von file_get_contents Echo, so dass die verschiedenen Tabellenelemente in Ihrem HTML vorhanden sind - es sieht jedoch chaotisch aus und ist nicht gültig HTML.

Eine Alternative mit DOMDocument und XPath - Beispiel.

$url='http://wttv.click-tt.de/cgi-bin/WebObjects/nuLigaTTDE.woa/wa/groupPage?championship=M%C3%BCnster+16%2F17&group=275320'; 

    /* simple variable to branch logic */ 
    $clonetable=true; 

    /* try to prevent errors */ 
    libxml_use_internal_errors(true); 

    /* create the DOMDocument object ready to receive html from remote url */ 
    $dom=new DOMDocument; 

    /* We need another instance of DOMDocument to clone nodes from source url */ 
    if($clonetable) $html=new DOMDocument; 

    /* use some of the defined properties for libxml */ 
    $dom->validateOnParse=false; 
    $dom->standalone=true; 
    $dom->strictErrorChecking=false; 
    $dom->recover=true; 
    $dom->formatOutput=false; 

    $dom->loadHTML(file_get_contents($url)); 

    /* Capture errors */ 
    $parse_errs=serialize(libxml_get_last_error()); 
    libxml_clear_errors(); 


    /* create an X-Path object ready to query the DOM */ 
    $xp=new DOMXPath($dom); 
    /* Query to find tables with given class */ 
    $col=$xp->query('//table[@class="result-set"]'); 

    /* 
     If the query succeeds, iterate through elements. 
     Technically you could use 1 xpath query to find the 
     table cells directly but you would have less control 
     over whether or not to display items etc etc 
    */ 
    if($col && !empty($col)){ 
     foreach($col as $index => $table){ 

      if($clonetable){ 

       /* Create a copy/clone of existing tables from remote page */ 
       $clone=$table->cloneNode(true); 
       /* Add these clones to the second DOMDocument instance */ 
       $html->appendChild($html->importNode($clone,true)); 

      } else { 

       /* Another xpath query to find the table cells and work with the data therein */ 
       $cells=$xp->query('tr/td',$table); 

       if($cells && !empty($cells)){ 
        foreach($cells as $cell){ 
         /* do something with the table cell data */ 
         echo $cell->nodeValue . '<br />'; 
        } 
       } 
      } 
     } 
     /* Display the cloned tables as they were in original document, minus CSS */ 
     if(is_object($html)) echo $html->saveHTML(); 
    } 
    if(!empty($parse_errs)){ 
     print_r($parse_errs); 
    } 
+0

Ihnen für Ihre Antwort danken. Ich kann den Code nicht zum Funktionieren bringen. Es gibt vier " Warnung: DOMDocument :: loadHTML():" Fehler in der 'loadHTML' Zeile und viele Hinweise" Hinweis: Verwendung von undefinierten Konstante BR - angenommen 'BR' "in der 'echo $ cell-> nodeValue.BR ; Zeile. – Tweakimp

+0

oh sorry - das 'BR' ist eine Konstante die ich lokal definiert habe - es ist ein '
' Tag. – RamRaider

+0

Es funktioniert jetzt, danke. Ich werde versuchen, mit den Daten, die ich jetzt habe, eine Tabelle zu bilden, bisher ist es nur eine Zeile für jeden Eintrag. – Tweakimp

Verwandte Themen