仓酷云

标题: PHP网页编程之百度晓得的php爬虫 [打印本页]

作者: 活着的死人    时间: 2015-2-3 23:33
标题: PHP网页编程之百度晓得的php爬虫
说说这一个月左右的学习情况和心得吧!我个人认为,既然决定了去做一件事,那就要以认真的态度去对待!既然决定来学习了,那不管当初是抱着怎样的心态来到这个培训班的,都要让自己认真的投入到学习中。    <?php
/*
  百度晓得爬虫!
经由咱们剖析,百度晓得是由静态网页构成。网址格局次要为http://zhidao.百度.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 )
   {
    $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.百度.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以前,我从未想过要做软件工程,即便是在去听过华育国际的关于软件工程的美好前景后,因为我一直都没有想过要与代码打交道,而是想学好所学专业,做个网络工程师或者是网络安全人员。
作者: 简单生活    时间: 2015-2-4 02:08
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
作者: 精灵巫婆    时间: 2015-2-5 13:37
学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。
作者: 飘灵儿    时间: 2015-2-16 20:10
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
作者: 透明    时间: 2015-2-18 12:04
作为一个合格的coder 编码的规范是必须,命名方面我推崇“驼峰法”,另外就是自己写的代码最好要带注释,不然时间长了,就算是自己的代码估计看起来都费事,更不用说别人拉。
作者: 冷月葬花魂    时间: 2015-2-25 23:45
当然这种网站的会员费就几十块钱。
作者: 小妖女    时间: 2015-3-8 10:15
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
作者: 深爱那片海    时间: 2015-3-10 07:40
不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。
作者: 因胸联盟    时间: 2015-3-17 07:12
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
作者: 分手快乐    时间: 2015-3-22 03:29
多看优秀程序员编写的代码,仔细理解他们解决问题的方法,对自身有很大的帮助。
作者: 海妖    时间: 2015-3-24 09:41
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
作者: 谁可相欹    时间: 2015-3-24 22:46
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
作者: admin    时间: 2015-3-26 06:07
找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。
作者: 飘飘悠悠    时间: 2015-3-26 13:09
最后介绍一个代码出错,但是老找不到错误方法,就是 go to wc (囧),出去换换气没准回来就找到错误啦。
作者: 不帅    时间: 2015-3-26 17:24
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
作者: 小女巫    时间: 2015-3-29 16:55
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
作者: 柔情似水    时间: 2015-4-2 06:13
,熟悉html,能用div+css,还有javascript,优先考虑linux。我在开始学习的时候,就想把这些知识一起学习,我天真的认为同时学习能够互相呼应,因为知识是相通的。
作者: 若天明    时间: 2015-5-9 04:01
对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。
作者: 老尸    时间: 2015-6-13 22:05
做为1门年轻的语言,php一直很努力。




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2