<?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.'个无效网页!'
;

?>

百度知道的php爬虫的更多相关文章

  1. 我们必须要知道的RESTful服务最佳实践

    看过很多RESTful相关的文章总结,参齐不齐,结合工作中的使用,非常有必要归纳一下关于RESTful架构方式了,RESTful只是一种架构方式的约束,给出一种约定的标准,完全严格遵守RESTful标 ...

  2. 隔壁小孩都要知道的Drupal配置

    i春秋作家:Arizona 原文来自:隔壁小孩都要知道的Drupal配置 隔壁小孩都要知道的Drupal配置 Drupal是一个开源的PHP内容管理系统,具有相当复杂的架构.它还具有强大的安全模型.感 ...

  3. 程序员必须要知道的Hadoop的一些事实

    程序员必须要知道的Hadoop的一些事实.现如今,Apache Hadoop已经无人不知无人不晓.当年雅虎搜索工程师Doug Cutting开发出这个用以创建分布式计算机环境的开源软...... 1: ...

  4. 【转载】在IT界取得成功应该知道的10件事

     在IT界取得成功应该知道的10件事 2011-08-11 13:31:30 分类: 项目管理 导读:前面大多数文章都是Jack Wallen写的,这是他的新作,看来要成为NB程序员还要不停的自我总结 ...

  5. 理工科应该的知道的C/C++数学计算库(转)

    理工科应该的知道的C/C++数学计算库(转) 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分析...这些或多或少与我们常用的软件息息相关,假如有一天你只需要这些大型软件 ...

  6. 你应该知道的10个奇特的 HTML5 单页网站

    网页设计师努力寻找新的方式来展现内容.其中一个大的趋势是单页网站,现在被世界上的一些大的品牌广泛采用,使用它们来为用户提供一个快速,干净和简单的而且​​美丽的网站. 下面是10个令人惊叹的单页 H​​ ...

  7. Git / 程序员需要知道的12个Git高级命令

    众所周知,Git目前已经是分布式版本控制领域的翘楚,围绕着Git形成了完整的生态圈.学习Git,首先当然是学习Git的基本工作流.相比于SVN等传统版本控制系统来说,Git是专为分布式版本控制而生的强 ...

  8. 你应该知道的RPC原理

    你应该知道的RPC原理 在学校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互 ...

  9. 希望早几年知道的5个Unix命令

    原文: http://spin.atomicobject.com/2013/09/09/5-unix-commands/ 希望早几年知道的5个Unix命令 使用*nix系统已经有一段时间了.但是还是有 ...

随机推荐

  1. 【Xamarin 挖墙脚系列:IOS 开发界面的3种方式】

    原文:[Xamarin 挖墙脚系列:IOS 开发界面的3种方式] xcode6进行三种基本的界面布局的方法,分别是手写UI,xib和storyboard.手写UI是最早进行UI界面布局的方法,优点是灵 ...

  2. ExecutorService生命周期

    ExecutorService接口继承了Executor接口,定义了一些生命周期的方法 public interface ExecutorService extends Executor { void ...

  3. mybatis源码分析(3)——SqlSessionManager类

    从上图可能看出,在 mybatis中,SqlSession的实现类有两个,其中SqlSessionManager类不但实现了SqlSession接口,同时也实现了SqlSessionFactory接口 ...

  4. Ajax长连接应用

    所谓的长连接,就是不断去发送请求,把请求阻塞在服务器端,每次超过请求时间就去重新发送请求,保持连接,随时获取服务器端的响应的数据 function connection(){ $.ajax({ typ ...

  5. 【转】iOS开发:开发证书知识点总结

    原文网址:http://www.jianshu.com/p/9c166a5e4930 1. Error: An App ID with identifier "*" is not ...

  6. 最短路(数据处理):HDU 5817 Ice Walls

    Have you ever played DOTA? If so, you may know the hero, Invoker. As one of the few intelligence car ...

  7. 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现[转]

    原文:http://www.cnblogs.com/leoo2sk/archive/2008/06/19/1225223.html 我们设计的分层架构,层与层之间应该是松散耦合的.因为是单向单一调用, ...

  8. FileUpload上传文件无法获取文件名

    原因:将FileUpload控件放到了UpdatePannel控件中了 解决办法:将FileUpload控件位置移动到UpdatePannel控件外面

  9. (转载)WindowsXP上安装和运行神器SqlMap的步骤

    在Windows XP上安装和运行SqlMap的步骤…… 1.首先下载SqlMap 点击下载. https://codeload.github.com/sqlmapproject/sqlmap/leg ...

  10. php连接mysql数据库练手

    <?php $servername = "localhost"; $username = "yosha"; $password = "leon0 ...