日期:2014-05-17 浏览次数:20480 次
<?PHP
//网络爬虫
include_once('Snoopy.class.php');
$spider = new Snoopy();
$spider->begin_url="http://blog.csdn.net/";
_spider($spider->begin_url);
print_r($spider->array_article);
function _spider($url){
global $spider;
$file="url.txt";
$fp=fopen($file,"a+");
$array_file=file($file);
$array = get_headers($url,1);
if(preg_match('/200/',$array[0])){//判断网页是否能正常访问
$spider->fetch($url); //抓取完整的网页
$spider->spider_text=$spider->results;
$spider->fetchlinks($url); //提取网页中的链接
$array_links=$spider->results;
$array_url=array(); //定义一个放url的数组,如果有重复的则舍弃,不重复的就添加
if(is_array($array_links)){
foreach($array_links as $value){
$is_match=strstr($value,'blog.csdn.net');
$is_match1=strstr($value,'#');
$is_match2=strstr($value,'@');
if($is_match!=false&&$is_match1==false&&$is_match2==false){ //如果链接是这个博客站的,导入文本中,否则不进行处理
$arr=explode(" ",$value);
$_value=$arr[0];
if(in_array($_value,$array_url)==false){
$array_url[]=$_value;
}
}
}
}
//print_r($array_url);
foreach($array_url as $value2){
$array2 = get_headers($value2,1);
if(preg_match('/200/',$array2[0])){
$is_match=strstr($value2,'/article/details/');
if($is_match!=false){
if(in_array($value2,$spider->array_article)==false){
static $num=1;
echo $num."...:".$value2."<br>";
$num=$num+1;
$_value2=$value2."\r\n";
if(in_array($value2,$array_file)==false){
$spider->array_article[]=$value2;
fwrite($fp,$_value2);
}
if(count($spider->array_article)>10000){
break;
}
}
}
else{
if(in_array($value2,$spider->array_category)==false){
$spider->array_category[]=$value2;
_spider($value2);
}
}
}
}
}
}
//echo get_magic_quotes_gpc();
?>