在尝试抓取网站时,对非对象调用成员函数find()


Call to a member function find() on a non-object while trying to scrape a website

这是我用来解析网站的代码。但是,有时我会收到错误消息Call to a member function find() on a non-object。为什么会发生这种情况?

require '../../img/simple_html_dom.php';
$searchterm = $_GET['id'];
$url = "http://bdlpdtr.com/listings-$searchterm";
$html = file_get_html( $url );
$posts = $html->find('div[class=fl lftSec mt45]');
foreach ( $posts as $post ) {
    $postedon = $post->find('span',6);
    $walkdate = $post->find('div[class=p]',1);
    $walkdatefull = $walkdate->find('span',0);
    $walktimefull = $walkdate->find('span',1);

在要刮取的页面中,如果Walkdate在那里,它正在工作,如果Walkdate不在那里,则显示上述错误。

谢谢,我从这个网站得到了答案。我刚刚添加了

if(!empty($walkdate)) {
    $walkdatefull = $walkdate->find('span',0);
    $walktimefull = $walkdate->find('span',1);
    }

它正在发挥作用。