我想刮一篇博客的标题文章,我写了下面的代码。我一直在想如何循环浏览每一页。
$dom = file_get_html('http://demos.appthemes.com/clipper/');
scrape('http://demos.appthemes.com/clipper/');
function scrape($URL)
{
$dom = file_get_html($URL);
foreach ($dom->find('.item-frame h1 a') as $items) {
$item = array('courseTitle' => $items->text());
var_dump($item);
}
}
for($pages = 0; $pages < 3;$pages++) {
if($next = $dom->find('a[class=page]', $pages)) {
$URL = $next->href;
$dom->clear();
unset($dom);
scrape($URL);
}
}
部分结果确实出现,但停留在错误Undefined variable: dom in on line 23
unset($dom);
导致$dom
变量未设置,并且在第二次循环迭代($pages == 1
)中对$dom->find
的调用失败。
我没有得到逻辑,但尝试删除$dom->clear(); unset($dom);
行。
希望能有所帮助。