首頁 php教程 php手册 百度知道的php爬虫

百度知道的php爬虫

Jun 21, 2016 am 08:52 AM
gt nbsp start this

/*
 百度知道爬虫!
经过我们分析,百度知道是由静态网页组成。网址格式主要为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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

解決方法:您的組織要求您更改 PIN 碼 解決方法:您的組織要求您更改 PIN 碼 Oct 04, 2023 pm 05:45 PM

解決方法:您的組織要求您更改 PIN 碼

Windows 11 上調整視窗邊框設定的方法:變更顏色和大小 Windows 11 上調整視窗邊框設定的方法:變更顏色和大小 Sep 22, 2023 am 11:37 AM

Windows 11 上調整視窗邊框設定的方法:變更顏色和大小

如何在 Windows 11 上變更標題列顏色? 如何在 Windows 11 上變更標題列顏色? Sep 14, 2023 pm 03:33 PM

如何在 Windows 11 上變更標題列顏色?

OOBELANGUAGE錯誤Windows 11 / 10修復中出現問題的問題 OOBELANGUAGE錯誤Windows 11 / 10修復中出現問題的問題 Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE錯誤Windows 11 / 10修復中出現問題的問題

Windows 11 上啟用或停用工作列縮圖預覽的方法 Windows 11 上啟用或停用工作列縮圖預覽的方法 Sep 15, 2023 pm 03:57 PM

Windows 11 上啟用或停用工作列縮圖預覽的方法

華為GT3 Pro和GT4的差異是什麼? 華為GT3 Pro和GT4的差異是什麼? Dec 29, 2023 pm 02:27 PM

華為GT3 Pro和GT4的差異是什麼?

Windows 11 上的顯示縮放比例調整指南 Windows 11 上的顯示縮放比例調整指南 Sep 19, 2023 pm 06:45 PM

Windows 11 上的顯示縮放比例調整指南

10種在 Windows 11 上調整亮度的方法 10種在 Windows 11 上調整亮度的方法 Dec 18, 2023 pm 02:21 PM

10種在 Windows 11 上調整亮度的方法

See all articles