当前位置:Gxlcms > PHP教程 > php抓取这个页面的内容,该如何解决

php抓取这个页面的内容,该如何解决

时间:2021-07-01 10:21:17 帮助过:20人阅读

php抓取这个页面的内容

需要抓取的部分已经用红线标出来,
只需要抓取第一页的就可以,
抓取页面:http://www.mafengwo.cn/yj/10206/2-0-1.html
一以前都是用这个类simple_html_dom.php,但是这个我用这个类抓取不出来了
------解决方案--------------------
可以用正則提取。
------解决方案--------------------
不至于吧?
include 'simple_html_dom.php';
$opts = array(
'http'=>array(
'user_agent' => $_SERVER['HTTP_USER_AGENT']
)
);
$context = stream_context_create($opts);
$url = 'http://www.mafengwo.cn/yj/10206/2-0-1.html';

$html = file_get_html($url, false, $context);
$div = $html->find('div.post-list ul');
foreach($div[0]->find('li') as $i=>$item) {
echo $item->find('img')[0]->src, PHP_EOL;
echo trim($item->find('h2')[0]->text()), PHP_EOL;
echo trim($item->find('div')[3]->text()), PHP_EOL;
//echo '**', $item->innertext(), PHP_EOL;
}

人气教程排行