百度知道的php爬虫
<?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爬虫的更多相关文章
- 我们必须要知道的RESTful服务最佳实践
看过很多RESTful相关的文章总结,参齐不齐,结合工作中的使用,非常有必要归纳一下关于RESTful架构方式了,RESTful只是一种架构方式的约束,给出一种约定的标准,完全严格遵守RESTful标 ...
- 隔壁小孩都要知道的Drupal配置
i春秋作家:Arizona 原文来自:隔壁小孩都要知道的Drupal配置 隔壁小孩都要知道的Drupal配置 Drupal是一个开源的PHP内容管理系统,具有相当复杂的架构.它还具有强大的安全模型.感 ...
- 程序员必须要知道的Hadoop的一些事实
程序员必须要知道的Hadoop的一些事实.现如今,Apache Hadoop已经无人不知无人不晓.当年雅虎搜索工程师Doug Cutting开发出这个用以创建分布式计算机环境的开源软...... 1: ...
- 【转载】在IT界取得成功应该知道的10件事
在IT界取得成功应该知道的10件事 2011-08-11 13:31:30 分类: 项目管理 导读:前面大多数文章都是Jack Wallen写的,这是他的新作,看来要成为NB程序员还要不停的自我总结 ...
- 理工科应该的知道的C/C++数学计算库(转)
理工科应该的知道的C/C++数学计算库(转) 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分析...这些或多或少与我们常用的软件息息相关,假如有一天你只需要这些大型软件 ...
- 你应该知道的10个奇特的 HTML5 单页网站
网页设计师努力寻找新的方式来展现内容.其中一个大的趋势是单页网站,现在被世界上的一些大的品牌广泛采用,使用它们来为用户提供一个快速,干净和简单的而且美丽的网站. 下面是10个令人惊叹的单页 H ...
- Git / 程序员需要知道的12个Git高级命令
众所周知,Git目前已经是分布式版本控制领域的翘楚,围绕着Git形成了完整的生态圈.学习Git,首先当然是学习Git的基本工作流.相比于SVN等传统版本控制系统来说,Git是专为分布式版本控制而生的强 ...
- 你应该知道的RPC原理
你应该知道的RPC原理 在学校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互 ...
- 希望早几年知道的5个Unix命令
原文: http://spin.atomicobject.com/2013/09/09/5-unix-commands/ 希望早几年知道的5个Unix命令 使用*nix系统已经有一段时间了.但是还是有 ...
随机推荐
- Linux搭建SVN 服务器
Linux搭建SVN 服务器 1 安装SVN 2 使用客户端连接 2.1 使用windows的客户端 2.2 使用Linux下的命令行 3 ...
- spring集成Apache的ActiveMQ
1.直接看优秀的博客 http://www.open-open.com/lib/view/open1435496659794.html
- linux 下 select 编程
linux 下的 select 知识点 unp 的第六章已经描述的很清楚,我们这里简单的说下 select 的作用,并给出 select 的客户端实例.我们知道 select 是IO 多路复用的一个最 ...
- bzoj1016
这道题主要利用了最小生成树的两个性质 最小生成树每种边权的数目固定不变 最小生成树每种边权带来的连通状况一定唯一 由于每种边权的只有不到10种,所以直接穷举然后乘法原理即可 ; type node=r ...
- bzoj1232
由题意知,最后要保留的边肯定都要被走过 来回一条边所花费的时间=2*边长+安慰边两端的牛所要花的时间和 总时间就等于所保留边来回的时间和+根节点时间: 不难想到做一下最小生成树即可 贪心可知,根一定选 ...
- BZOJ_4196_[NOI2015]_软件包管理器_(树链剖分)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4196 给出一棵树,树上点权为0或1.u权值为1的条件是从根节点到u路径上的所有点权值都为1.u ...
- extjs form 取值 赋值 重置
一.从form中获取field的三个方法: 1.Ext.getCmp('id'); 2.FormPanel.getForm().findField('id/name'); 3.Ext.get('id/ ...
- devi into python 笔记(六)正则表达式 原始字符串
字符串函数replace: #string.replace: #字符串的replace方法:替换子串,不改变原来的字符串 s = "broad road" #打印出来会发现不单单是 ...
- Naive and Silly Muggles
Problem Description Three wizards are doing a experiment. To avoid from bothering, a special magic i ...
- Kicad中批量添加过孔
布线按V即可插入过孔,但在铺铜,或大电流走线时,有时需要手动添加一些过孔. 但Kicad里面并没有这样的菜单,最后搜索到,要添加过孔时,需要先建立一个单过孔的封装. 然后插入这个封装到PCB.然后修改 ...