php抓取页面函数

2012/11/13 1845点热度 0人点赞 0条评论

curl

function GetSources($Url,$User_Agent='',$Referer_Url='') //抓取某个指定的页面
{
//$Url 需要抓取的页面地址
//$User_Agent 需要返回的user_agent信息 如“baiduspider”或“googlebot”
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $Url);
curl_setopt ($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt ($ch, CURLOPT_REFERER, $Referer_Url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$MySources = curl_exec ($ch);
curl_close($ch);
return $MySources;
}
$User_Agent = "baiduspider+(+http://www.baidu.com/search/spider.htm)";
$Referer_Url = 'http://www.baidu.com.com/';
$file = GetSources($Url,$User_Agent,$Referer_Url);

如果不支持curl请替换


$ctx = stream_context_create(array(
'http' => array(
'timeout' => 10 //设置一个超时时间,单位为秒
)
)
);
$file = file_get_contents($Url, 0, $ctx);

幸运猪头

保持饥渴的专注,追求最佳的品质

文章评论