本帖最后由 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];
}
?>
http://www.google.com.hk
http://www.google.com.hk/search?q=".urlencode($keyword)."&&oe=gb2312&hl=zh-CN&safe=off&num=20&start=
http://www.google.com.hk/search?q=".urlencode($keyword)."&&oe=gb2312&hl=zh-CN&safe=off&num=20&start=
原来google搜索是跟百度不一样的。。所以重新写了。
结合网上修改而来,可用。{:4_51:} 算作伪原创吧。
谷歌百度均已解决。。接下来研究别人写的exp了……