2016-08-22 35 views
0

这里是我的代码:使用file_get_html和PHP Simple HTML DOM获取整个页面的一部分。

<?php 

include('simple_html_dom.php'); 

$html = file_get_html('http://www.bbc.com'); 

    $html = strstr($html, '<span class="module__title__link tag tag--feature">Rio 2016</span>'); 
    $html = strstr($html, '<span class="module__title__link tag tag--feature">Editor’s Picks</span>', true); 
    $html = substr($html, 0, -1); 

foreach($html->find('div.media__content') as $article) { 

    echo $article; } 

?> 

我使用strstr到该页面的返回一部分,当我echo $html;一切正常。

,但我得到:Fatal error: Call to a member function find() on a non-object

我该怎么办?

UPDATE:解释一下我想做的事:

1:解析 “bbc.com” 为html
2:修剪的那
3部分:分配部分,$ HTML
4 :查找的部分,并将其分配给字符串,如:
$item['description'] = $article->find('a', 0)->href;

+1

,因为你转换$ HTML对象的字符串 –

+0

如何解决它? –

+1

从代码 –

回答

-1

$ HTML是一个字符串不是一个对象,因此,你不能调用一个方法就可以了。 foreach循环里面

+0

如何解决它。 –

+0

@MehdiBoveiri我不知道,你使用库来解析html吗? – vincenth

+0

我使用PHP简单的HTML DOM解析器:http://simplehtmldom.sourceforge.net/ –

0

使用STR

<?php 
    include('simple_html_dom.php'); 
    $html = file_get_html('http://www.bbc.com'); 

     foreach($html->find('div.media__content') as $article) { 

     echo strstr($article, 'What You Want To Search'); 

      } 

     ?> 
+0

echo strstr($ article,' Rio 2016'); 它返回null;我认为是错误的从我的问题,解释我想要做什么: 1:解析“bbc.com”为html 2:修剪部分 3:将部分分配给$ html 4:找到部分和把它分配给字符串: '$ item ['description'] = $ article-> find('a',0) - > href;' –