2016-08-11 4 views
0

私はsimple_html_dom.phpについて学んでいる、 私はいくつかのクラスでh1のコンテンツを取得しようとします。単純なhtml dom h1ヘッダーを

<h1 class="entry-title">example for the header</h1> 

ここでは、コンテンツを取得したいウェブサイトの生のhtmlファイルです。ここ

<header class="entry-header"> 
    <div class="entry-meta"> 
     <span class="cat-links"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="category tag">News</a></span> 
    </div> 
    <h1 class="entry-title">example for the header</h1> 
    <div class="entry-meta"> 
     <span class="entry-date"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="bookmark"><time class="entry-date" datetime="2016-08-11T11:54:07+00:00">11 August 2016</time></a></span> 
     <span class="byline"><span class="author vcard"><a class="url fn n" href="https://xxxxx/2016/08/11/xxxxxxx" rel="author">wndwnrt</a></span></span>   
     <span class="comments-link"><a href="https://xxxxx/2016/08/11/xxxxxxx">1 Comment</a></span> 
    </div> 
</header> 

H1クラス=「エントリタイトル」コンテンツ(ヘッダの一例)

<?php 
require_once __DIR__.'/simple_html_dom.php'; 
$html = new simple_html_dom(); 
$html->load_file('https://xxxxx/2016/08/11/xxxxxxx'); 
$header_1 = $html->find('h1[class="entry-title"]')->innertext; 
?> 
<table border="1"> 
<thead> 
    <tr> 
    <th><?php echo $header_1; ?></th> 
    </tr> 
</thead> 
</table> 

を得るために私のコード私は、コードを実行すると、結果がエラーである:

Trying to get property of non-object 

誰でもどこでエラーが発生するのかを教えてください。私は何をすべきか? ありがとうございます。

+0

var_dump($ header_1)これが何であるかを見ると、 –

+0

@amanの結果として "NULL"が表示されます。ありがとう –

答えて

4

はい、find関数に1つの引数しか渡していないため、このエラーが表示されます。あなたも、あなたが取得しようとしているH1の数を渡す必要があるため

$header_1 = $html->find('h1[class="entry-title"]',0)->innertext 

$header_1 = $html->find('h1[class="entry-title"]')->innertext 

今これを試してみてください!

+0

[ドキュメント](http://simplehtmldom.sourceforge.net/manual.htm#frag_find_basic)に示されているとおりです。 – Phylogenesis

+0

@ StackB00m大丈夫、ありがとう。私はそれについて恋しい。 –

関連する問題