PHP CURL抓取百度收录量信息方法(含近总收录量近一周近一月)
最近在给我们笨牛网工具箱系统(https://tool.bnxb.com)写一套查询网站收录量的程序,就用到PHP CURL获取百度网站信息。详细代码如下:
<?php //构建一个调用函数 function _get_header($url,$preg) { if(function_exists('curl_init')){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14 (.NET CLR 3.5.30729)'); curl_setopt($curl, CURLOPT_VERBOSE, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($curl, CURLOPT_AUTOREFERER, true); $data=curl_exec($curl); preg_match($preg,$data,$num); $baidunum = $num[1]; return $baidunum; } } //使用自构建的函数进行查询输出 $pregbaidu = '/该网站共有.*?>(.*)</'; $a = "https://www.baidu.com/s?wd=site%3Abnxb.com"; $baidutotal = _get_header($a,$pregbaidu); echo $baidutotal; //取系统时间和7天前1月前时间UNIX时间戳 $date = time(); $date7before = $date - 604800; $date1month = $date - 2592000; $date1day = $date - 86400; //近7天数据 $pregbaidu7day = '/找到相关结果数约(.*)个/'; $b = $a."&gpc=stf%3D".$date7before."%2C".$date."%7C"; $baidu7day = _get_header($b,$pregbaidu7day); echo $baidu7day; //近1月数据 $pregbaidu1month = '/找到相关结果约(.*)个/'; $c = $a."&gpc=stf%3D".$date1month."%2C".$date."%7C"; $baidu1month = _get_header($c,$pregbaidu1month); echo $baidu1month; //近1天数据 $pregbaidu1day = '/找到相关结果约(.*)个/'; $d = $a."&gpc=stf%3D".$date1day."%2C".$date."%7C"; $baidu1day = _get_header($d,$pregbaidu1day); echo $baidu1day; ?>
顶(0)
踩(0)
- 最新评论