搜索
首页php教程php手册百度知道的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 , '

') ;<br>   $start = $this->GetStart( $start ) ;<br>   $end = strpos( $this->content , '' , $start ) ;
  $title = substr( $this->content , $start , $this->$end-$start ) ;
  if ( strpos( $title , '_百度知道' , 1 )   {
   return false;
  }
  return ture ;
 }
 
 function GetTitle()
 {
  $start = strpos( $this->content , '') ;<br>   if ( $start > 0 )<br>   {<br>    $start = $this->GetStart( $start ) ;<br>    $end = strpos( $this->content , '' , $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 , '' , $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.'个无效网页!' ;

?>



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能