随笔 - 106  文章 - 0 评论 - 6 阅读 - 18万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

<?php 

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 ) 
   
      $start = $this->GetStart( $start ) ; 
      $end = strpos( $this->content , '</pre>' , $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 < $sndqid ; $i++ ) 

  $url = $standurl.$i.$html ; 
  if ( $TestSpider->GetContent ( $url ) ) 
 
    echo '<br>正在爬取编号为'.$i.'的网页<br>' ; 
    $TestSpider->GetTitle() ; //得到网页标题,不用显示了 
    echo '<font color="green">问题:</font><font color="red"><a target="_blank" href="'.$url.'"> '.$TestSpider->GetQTitle().'</a></font><br>' ; //得到问题题目 
    echo '<font color="green">问题具体内容:</font>'.$TestSpider->GetQContent().'</font><br>' ; //得到问题内容,有可能不存在 
    echo '<font color="green">问题补充说明:</font>'.$TestSpider->GetQsuply().'</font><br>' ; //问题补充说明,有可能不存在 
    while ( ($answer = $TestSpider->GetAnswer()) != NULL )  
   
      echo '<font color="green">问题答案:</font>'.$answer.'</font><br>' ; //得到答案。有可能没有答案! 
   
    ob_flush() ; 
    flush() ; 
 
  else 
 
    echo '<p>错误了<a target="_blank" href="'.$url.'" style= "color:#ff0000">'.$url.'</a></p>' ; 
    $NoUse++ ; 
 

$time_end = microtime_float(); 
$time = $time_end - $time_start; 
$i = $i-$Startqid ; 
echo '<p>爬取'.$i.'个网页用时'.$time.'秒</p>其中跳过'.$NoUse.'个无效网页!' ; 

?>

posted on   鸟叔手擒大数据  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示