百度知道的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 , '
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '
$title = substr( $this->content , $start , $this->$end-$start ) ;
if ( strpos( $title , '_百度知道' , 1 ) {
return false;
}
return ture ;
}
function GetTitle()
{
$start = strpos( $this->content , '
if ( $start > 0 )
{
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '
$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 , '' , $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 , '' , $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 , '' , $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 )
{
$start = $this->GetStart( $start ) ;
$end = strpos( $this->content , '' , $start ) ;
$this->CurPosition = $end ;
return substr( $this->content , $start , $end-$start ) ;
}
return NULL ;
}
}
ini_set('max_execution_time', '0');
$TestSpider = new spider() ;
$Startqid = 1000001 ;
$sndqid = 1000051 ;
$standurl = 'http://zhidao.baidu.com/question/' ;
$html = '.html' ;
$url ;
$NoUse = 0 ;
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
$answer ;
for ($i = $Startqid ; $i {
$url = $standurl.$i.$html ;
if ( $TestSpider->GetContent ( $url ) )
{
echo '
正在爬取编号为'.$i.'的网页
' ;
$TestSpider->GetTitle() ; //得到网页标题,不用显示了
echo '问题: '.$TestSpider->GetQTitle().'
' ; //得到问题题目
echo '问题具体内容:'.$TestSpider->GetQContent().'
' ; //得到问题内容,有可能不存在
echo '问题补充说明:'.$TestSpider->GetQsuply().'
' ; //问题补充说明,有可能不存在
while ( ($answer = $TestSpider->GetAnswer()) != NULL )
{
echo '问题答案:'.$answer.'
' ; //得到答案。有可能没有答案!
}
ob_flush() ;
flush() ;
}
else
{
echo '
错误了'.$url.'
' ;$NoUse++ ;
}
}
$time_end = microtime_float();
$time = $time_end - $time_start;
$i = $i-$Startqid ;
echo '
爬取'.$i.'个网页用时'.$time.'秒
其中跳过'.$NoUse.'个无效网页!' ;?>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Windows 11에서 창 테두리 설정을 조정하는 방법: 색상 및 크기 변경

Windows 11에서 제목 표시줄 색상을 변경하는 방법은 무엇입니까?

Windows 11에서 작업 표시줄 축소판 미리 보기를 활성화 또는 비활성화하는 방법
