curl采集 根据关键词 获取雅虎竞价排名

时间:2023-03-08 17:42:41

之前写过curl批处理采集数据,这里贴上完整版本,代码很简单,废话不说,上代码,新手欢迎指教!!!

代码只写到 获取到链接了,至于排名 后边数组的键不就是排名喽。。。

 <?php
/**
* Based on yahoo access to data
*
* @author chujiu <527891885@qq.com>
* @copyright 2014.04.26 By chujiu
* @version 0.2.1 2014.04.26
*/ class DataCollectionRank { const PAGE = 10;
public $path = '';
public $main = 91; // 添加curl句柄 返回资源
private function _gather_data($keyword) {
if(empty($keyword)) {
return '';
}
$chs = array(); // 句柄
$mh = curl_multi_init();
for( $i=1; $i<=$this->main; $i+=self::PAGE ) {
$url = 'http://search.yahoo.co.jp/search?p='.urlencode($keyword).'&tid=top_ga1_sa&ei=UTF-8&aq=-1&oq='.urlencode($keyword).'&pstart=1&fr=top_ga1_sa&b='.$i;
$ch = curl_init();
//设置选项
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_AUTOREFERER => true
)
);
curl_multi_add_handle($mh, $ch); // 添加批处理句柄
$chs['handle'][$i]['ch'] = $ch;
$chs['handle'][$i]['url'] = $url;
}
$chs['mh'] = $mh;
return $chs;
} // 处理CURL请求
public function exec_curl_get_data($keyword, $path) {
$error = '';
$this->path = $path;
$chs = $this->_gather_data($keyword);
if(empty($chs)) return '';
// 执行批处理句柄
$active = null;
do {
$mrc = curl_multi_exec($chs['mh'],$active);
//$info = curl_multi_info_read($chs['mh']);
} while ($active > 0);
// 获取数据
$responses = array();
foreach($chs['handle'] as $k=>$ch){
if(curl_error($ch['ch'])){
$error .= "\n".'error提示:'.curl_error($ch['ch']).'-------URL:'.$ch['url'].'--------时间:'.date('Y-d-m H:i:s',time())."\n";
} else {
$responses[$k]['data'] = curl_multi_getcontent( $ch['ch'] );
} //curl_multi_info_read($mh);
// close current handler
curl_multi_remove_handle($chs['mh'], $ch['ch']);
curl_close($ch['ch']);
}
//关闭curl 批处理
curl_multi_close($chs['mh']);
$str = '';
if($error != '') {
$this->_writeFile('get_rank_log.txt', $error, 'ab+');
}
foreach ($responses as $val) {
if(!empty($val['data'])) {
$str.= $this->_get_keyword_link_preg($val['data']);
}
}
$str = substr($str, 0 ,-1);
$contents = explode('|', $str);
return $contents;
} // 过滤数据 获取链接
private function _get_keyword_link_preg ($str) {
$res = '';
if(empty($str)) {
return '';
}
$arr = explode('<div id="web">', $str);
$arr1 = explode('<div id="posS" class="spns">', $arr[1]);
$arr2 = preg_replace('#<div id=\"pg\">[\s\S]+#', '', $arr1[0]);
$arr3 = preg_replace('#<div id=\"rel\">[\s\S]+#', '', $arr2);
$arr4 = preg_replace('#<em>[\s\S]+?</em>#', '', $arr3);
if(preg_match_all('#href=\"(.*?)\">#',$arr4,$arr5) !== false) {
foreach($arr5[1] as $val) {
$res.= urldecode($val).'|';
}
}
return $res;
} // 写入文件
public function _writeFile($fileName, $data, $method="rb+", $iflock=1, $check=1, $chmod=1){
$check && @strpos($this->path.'/'.$fileName, '..')!==false && exit('403 Forbidden!');
@touch($this->path.'/'.$fileName);
$handle = @fopen($this->path.'/'.$fileName, $method);
if($iflock) {
@flock($handle,LOCK_EX);
}
$fw = @fwrite($handle,$data);
if($method == "rb+") ftruncate($handle, strlen($data));
fclose($handle);
$chmod && @chmod($this->path.'/'.$fileName,0777);
}
}
?>
 function array_unique_fb($array){
$temp = array();
$data = array();
foreach ($array as $value){
$value = join(",",$value); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
$temp[] = $value;
}
$temp = array_flip(array_flip($temp)); //去掉重复的字符串,也就是重复的一维数组
foreach ($temp as $k => $value){
$temp[$k] = explode(",",$value); //再将拆开的数组重新组装
}
foreach ($temp as $key => $value) {
$data[$key]['keyword'] = $value[0];
$data[$key]['domain'] = $value[1];
}
return $data;
}