php获取google搜索结果

本帖最后由 jjf012 于 2012-6-25 23:08 编辑


<?php
$keyword='inurl:edu.cn/news.php?id=';
$startpage = 1;
$lastpage = 2;
header('Content-Type: text/html; charset=utf-8');
$cookie_file = dirname(__FILE__).'/cookie.txt';
//$cookie_file = tempnam("tmp","cookie");
 
//先获取cookies并保存
$url = "http://www.google.com.hk";
$ch = curl_init($url); //初始化
curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
curl_exec($ch);
curl_close($ch);

for($i=$startpage;$i<$lastpage;$i++){
	$array = readGoogle($keyword, $i,0);//1表示只获取主机名,反之则获取网址链接
		foreach ($array as $key=>$url){
		$url_list = file('url.txt');
		if (in_array("$url\r\n", $url_list)){
			echo "[-]  Links repeat, in page $i line $key<br />\n";
		}else{
			$fp = @fopen('url.txt', 'a'); //写入文件前判断是否重复
			@fwrite($fp, $url."\r\n");
			@fclose($fp);
		}
	}
}
////使用上面保存的cookies再次访问
function readGoogle($keyword,$nowpage,$sign){
$url = "http://www.google.com.hk/search?q=".urlencode($keyword)."&&oe=gb2312&hl=zh-CN&safe=off&num=20&start=".$nowpage;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); //使用上面获取的cookies
$response = curl_exec($ch);
curl_close($ch);
preg_match_all("/<cite>(.*?)<\/cite>/" ,$response, $res);
$num = count($res[1]);
for($i=0;$i<$num;$i++){
	$res[1][$i] = str_replace('<b>','',$res[1][$i]);
	$res[1][$i] = str_replace('</b>','',$res[1][$i]);
	if($sign==1){
	$row = explode('/', $res[1][$i]);
	$res[1][$i] = $row[0];
	}
}
return $res[1];
}

?>

原来google搜索是跟百度不一样的。。所以重新写了。
结合网上修改而来,可用。{:4_51:} 算作伪原创吧。
谷歌百度均已解决。。接下来研究别人写的exp了……

不太清楚你用PHP实现这种功能的目的是??{:4_57:}

**回复 2# winy **

批量日站

网站猎手,的exe程序个人感觉不错,批量从百度获取url地址。然后导出,也能批量日。

不太清楚你用PHP实现这种功能的目的是??
winy 发表于 2012-6-26 00:31 [[img]http://www.90sec.org/images/common/back.gif[/img\]](http://www.90sec.org/redirect.php?goto=findpost&pid=32525&ptid=2810)

看能不能全自动日站。。

谷歌被天朝之后就不太行勒

**回复 3# darkmoon **

关键是获取了gg的结果,怎么用php自动分析结果中的站点让不让日?期待LZ下一步的动作{:4_97:}