php 实现使用curl模拟百度蜘蛛进行采集
发布时间:2020-05-25 08:00:44 所属栏目:PHP 来源:互联网
导读:php 实现使用curl模拟百度蜘蛛进行采集
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 //实现使用curl模拟百度蜘蛛进行采集
class Curlcontent{
protected function _GetContent( $url )
{
$this->ch = curl_init();
$this->ip = '220.181.108.'.rand(1,255); // 百度蜘蛛
$this->timeout = 15;
curl_setopt($this->ch,CURLOPT_URL,$url);
curl_setopt($this->ch,CURLOPT_TIMEOUT,0);
//伪造百度蜘蛛IP
curl_setopt($this->ch,CURLOPT_HTTPHEADER,array('X-FORWARDED-FOR:'.$this->ip.'','CLIENT-IP:'.$this->ip.''));
//伪造百度蜘蛛头部
curl_setopt($this->ch,CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($this->ch,CURLOPT_HEADER,0);
curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,$this->timeout);
curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER,false);
$content = curl_exec($this->ch);
if($content === false)
{//输出错误信息
$no = curl_errno($this->ch);
switch(trim($no))
{
case 28 : $this->error = '访问目标地址超时'; break;
default : $this->error = curl_error($this->ch); break;
}
echo $this->error;
}
else
{
$this->succ = true;
return $content;
}
}
public function getcurl($url){
return $this->_GetContent($url);
}
}
$api = "https://www.maihuangjin.com/mobile/";
$Curlcontent = new Curlcontent();
$data = $Curlcontent->getcurl($api); 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
