日期:2011-11-16 浏览次数:20642 次
<?php
/*
百度知道爬虫!
经过我们分析,百度知道是由静态网页组成。网址格式主要为http://zhidao.baidu.com/question/(编号).html,其中编号便是该问题的编号(PID),按照时间逐个编号的,由于某些问题的删除或者某种原因,可能编号不连续。当我们发现该问题删除时,可以跳过该问题继续。
*/
class spider
{
private $content ;
private $contentlen ;
private $BestAnswer ;
private $CurPosition ;
function GetStart( $iStart )
{
return strpos( $this->content , '>' , $iStart )+1 ;
}
function GetContent ( $url )
{
$this->content = file_get_contents($url);
$this->contentlen = strlen( $this->content ) ;
$start = strpos( $this->content , '<title>') ;
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '</title>' , $start ) ;
$title = substr( $this->content , $start , $this->$end-$start ) ;
if ( strpos( $title , '_百度知道' , 1 ) < 1 )
{
return false;
}
return ture ;
}
function GetTitle()
{
$start = strpos( $this->content , '<title>') ;
if ( $start > 0 )
{
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '</title>' , $start ) ;
$this->CurPosition = $end ;
return substr( $this->content , $start , $end-$start ) ;
}
return NULL ;
}
function GetQTitle()
{
$start = strpos( $this->content , 'span class="question-title"' , $this->CurPosition ) ;
if ( $start > 0 )
{
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '</span>' , $start ) ;
$this->CurPosition = $end ;
return substr( $this->content , $start , $end-$start ) ;
}
return NULL ;
}
function GetClassFly()
{
;
}
function GetQContent()
{
$start = strpos( $this->content , 'pre id="question-content"' , $this->CurPosition ) ;
if ( $start > 0 )
{
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '</pre>' , $start ) ;
$this->CurPosition = $end ;
return substr( $this->content , $start , $end-$start ) ;
}
return NULL ;
}
function GetQsuply()
{
$start = strpos( $this->content , 'id="question-suply"' , $this->CurPosition ) ;
if ( $start > 0 )
{
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '</pre>' , $start ) ;
$this->CurPosition = $end ;
return substr( $this->content , $start , $end-$start ) ;
}
return NULL ;
}
function GetAnswer()
{
$start = strpos( $this->content , 'class="reply-text mb10"' , $this->CurPosition ) ;
if ( $start > 0 )
{
$s