为了给同事批量抓一些素材,写了段程序,自动抓100多个页面的5000多张图,对方站点防盗链开启了,只能用图片所在页面地址做reffer才能抓图,于是用curl伪造reffer。函数贴出来,备忘,函数本身可以模仿Baidu蜘蛛,由同事乐乐友情提供,我稍稍修改了一下,感谢一下乐乐同学。
function baiduSpider($url,$reffer){ $ch = curl_init(); $user_agent = “Baiduspider+(+http://www.baidu.com/search/spider.htm)”;//这里模拟的是百度蜘蛛 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, $reffer);//这里写一个来源地址,可以写要抓的页面的首页 curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); $temp=curl_exec($ch); return $temp; }
|