我的个人学习笔记-赵新朋:新时代农民工

  • 首页
  • 日常备忘
  • 服务器类
  • 我的收藏
  • 未分类
  • 国外VPS
欢迎你来到猪窝!
  1. 首页
  2. 建站技术
  3. 正文

php抓取页面函数

2012/11/13 1665点热度 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);

标签: 暂无
最后更新:2012/11/13

Kratos

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

点赞
< 上一篇
下一篇 >

文章评论

取消回复
2023年 2月
一 二 三 四 五 六 日
 12345
6789101112
13141516171819
20212223242526
2728  
« 8月    

    COPYRIGHT © 2021 赵新朋:新时代农民工. ALL RIGHTS RESERVED.

    THEME KRATOS MADE BY VTROIS

    陕ICP备15003432号-1