php使用file_get_contents函数抓取页面信息

一般我都习惯于使用curl进行页面的抓取,因为curl可以模拟一系列动作,

 例如登录一个网站,跳转到信息抓取页面,通过正则表达式或者一系列方法抓取自己想要的信息数据。

假如某个站点没有这么复杂,可以通过直接访问得到结果的话

不如采用 file_get_contents 这个函数来抓取页面数据。

例如,访问54master论坛,想抓取首页里 所有h3标记内的元素。

$url="http://bbs.54master.com";
$contents=@file_get_contents($url);
//preg_match_all("/<p class=\"right forumcount\">(.*?)<\/p>/is",$contents,$content);
preg_match_all("/<h3>(.*?)<\/h3>/is",$contents,$content);
print_r($content[0]);


原文地址:https://www.cnblogs.com/tianxin2001x/p/1657608.html