2016-07-13 14 views
0

コードは以下のとおりです。私は、file_get_contentsの後にリンクされたURLのid結果セットでテーブルを解析したいと思います。これは正しい方法ですか?私はPHP、html、javascriptの新機能ですので、簡単な手順を説明してください。私にとって難しいのは、html、php、jsを組み合わせて、エラーなく動作させることです。オブジェクト最初のコードのHTMLCollectionエラー

私はこれをPHPファイルとして保存して開くと、[object HTMLCollection]としか言いません。どうして?ここでの問題は何ですか?あなたがメッセージ「htmlCollection」を得る理由ですが、リモートサイトからのHTMLはHTMLページに存在しないので、それが空になるノードのコレクションを返すためにJavascriptを使用しているあなたのコードで

<!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

あなたは '$のvar'に' DOMDocument'を使用すると、あなたは〜 'DOMXPath'も – RamRaider

答えて

0

とにかくコレクション。表示されたメソッドを使用することができますが、さまざまなテーブル要素がHTMLに存在するように、file_get_contentsからの応答をエコーする必要があります。しかし、これは面倒ですが、有効なhtmlではありません。

代わりに、DOMDocumentおよびXPath - 例を使用してください。

$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

は、あなたの答えをありがとう役に立つかもしれませんする必要があるとして、文書を処理しなければなりません。私は働くコードを得ることができません。 '' loadHTML'行のエラーと多くの注釈」注意:定義されていない定数BRの使用 - 'echo $ cell-> nodeValue.BRの 'BR' 'の使用; '行。 – Tweakimp

+0

ああ申し訳ありません - 'BR'は私がローカルで定義した定数です - それは'
'タグです。 – RamRaider

+0

これは今、おかげさまで動作します。私が今持っているデータを使ってテーブルを作成しようとします。これまでのところ、すべてのエントリに対して1行だけです。 – Tweakimp

関連する問題