- 论坛徽章:
- 0
|
在网上看到很多简单的采集教程,尤其是针对图书网站的比较多,但附带实例的并不多,在看了一篇针对八路中文网的抓取分析后,决定针对这个网站,写一个简单的抓取教程,并附带实例。由于俺偷懒,文中很多分析都是来自《利用PHP制作简单的内容采集器》,俺只是进一步优化了他的流程,并完成了代码实例的编写。
采集程序其实并不难做,只要分析清楚流程,然后使用合适的正则来取到你想要的内容就可以了。废话不说了,教程开始:
1.分析入口:
多打开几本书后,可以发现书名的基本格式是:http://www.86zw.com/Book/书号/Index.aspx。于是得出:
- $BookId='1888';
- $index="http://www.86zw.com/Book/".$BookId."/Index.aspx";//组合书目首页URL
复制代码
2.打开页面:
- $contents=file_get_contents($index);
复制代码
3.抓取图书信息页:
- //抓取图书相关信息
- preg_match_all("/<div id=\"CrBookTitle\"><span class=\"booktitle\">(.*)<\/span><\/div>/is",$contents,$Arraytitle);
- preg_match_all("/【<a href=\"(.*)\"><font color=\"#CC0000\">点击阅读<\/font><\/a>】/is",$contents,$Arraylist);
- unset($contents);
- $title=$Arraytitle[1][0];//书名
- $list="http://www.86zw.com".trim($Arraylist[1][0]);//列表页URL
复制代码
4.创建保存目录及文件:
- //生成文本文档名称
- $txt_name=$title.".txt";
- Creatdir($BookId);//创建图片文件夹
- writeStatistic($title."\r\n",$txt_name);//图书标题写入文本文件
复制代码
5.进入列表页:
- //进入列表页
- $list_contents=file_get_contents($list);
复制代码
6.抓取列表页章节:
- //进入列表页
- //分章节抓块
- preg_match_all("|<div id=\"NclassTitle\">(.*) 【<a href=\"(.*)\">分卷阅读<\/a>】<\/div>(.*)<div id=\"ListEnd\"><\/div>|Uis",$list_contents,$Block);
- //计算总章节数
- $regcount=count($Block[0]);
复制代码
7.分章节进行抓取:
- //进入章节
- for($pageBookNum=0;$pageBookNum<$regcount;$pageBookNum++){
- unset($Zhang);
- unset($list_url);
- $Zhang=$Block[1][$pageBookNum];//章节标题
- writeStatistic('章节:'.($pageBookNum+1).' '.$Zhang."\r\n",$txt_name);//章节标题写入文本文件
- preg_match_all("|<li><a href=\"(.*)\" title=\"(.*)\">(.*)<\/a><\/li>|Uis",$Block[3][$pageBookNum],$list_url);
- //进入页面
- for($ListNum=0;$ListNum<count($list_url[1]);$ListNum++){
- unset($Book_url);
- unset($Book);
- unset($Book_contents);
- unset($Book_time);
- unset($Book_title);
- $Book_time=$list_url[2][$ListNum];//小章节更新信息
- $Book_title=$list_url[3][$ListNum];//小章节标题
- $Book_url=preg_replace("'Index.shtm'si",$list_url[1][$ListNum],$list);//小章节链接URL
- writeStatistic(($ListNum+1).'.'.$Book_title.'-'.$Book_time."\r\n",$txt_name);//小章节标题写入文本文件
- $Book=file_get_contents($Book_url);
- //抓取图书内容
- preg_match_all("/<div id=\"BookText\">(.*)<iframe id=hkwxc/is",$Book,$Arraycontents);
- $Book_contents=preg_replace("|<div style='display:none'>.*<\/div>|Uis",'',$Arraycontents[1][0]);
- //$Book_contents=preg_replace("|<br />|Uis",'\n\r',$Book_contents);
- //$Book_contents=preg_replace("|<br>|Uis",'\n\r',$Book_contents);
- $Book_contents=preg_replace("| |Uis",' ',$Book_contents);
- $Book_contents=strip_tags($Book_contents);
- //判断图片页面
- if (preg_match ("/<div align=\"center\"><img src=\".*\" id=\"imgbook\" name=\"imgbook\" border=\"0\" \/><\/div>/i", $Book_contents)) {
- //取图片URL
- preg_match_all("|<div align=\"center\"><img src=\"(.*)\" id=\"imgbook\" name=\"imgbook\" border=\"0\" \/><\/div>|Uis",$Book_contents,$images);
- //取图片
- for($ImageNum=0;$ImageNum<count($images[1]);$ImageNum++){
- unset($Image_url);
- $Image_url="http://www.86zw.com".trim($images[1][$ImageNum]);
- $New_url='image/'.$BookId.'/'.time().'.gif';
- //复制图片并生成图片连接
- if (copy($Image_url, $New_url)){
- $Book_contents.="<img src=$New_url>";
- }
- }//取图片结束
- }//图片判断结束
-
- writeStatistic($Book_contents,$txt_name);//内容写入文本文件
- }//页面循环结束
- }//章节循环结束
复制代码
两个使用的函数:
- /**
- * 将内如写入指定文件包
- *
- * 参数: string $sql : 写入的内容
- string $txt_name : 指定文件名
- * 返回: void
- * 作用域: public
- * 日期: 2007-11-29
- */
- function writeStatistic($sql,$txt_name){
- $filename="txt_packet/".$txt_name;//注意修改文件的路径
- if (file_exists($filename)) {
- $fp=fopen($filename,"a");
- }else{
- $fp=fopen($filename,"w");
- }
-
- $text=$sql;
- fwrite($fp,$text);
- fclose($fp);
- }
- /**
- * 创建文件夹
- *
- * 参数: string $BookId : 指定文件夹名
- * 返回: void
- * 作用域: public
- * 日期: 2007-11-29
- */
- function Creatdir($BookId){
- $filename="image/".$BookId;//注意修改文件的路径
- if (!file_exists($filename)) {
- mkdir($filename,0777);
- }
- }
复制代码
自此完成一本书的简单采集。
写这个就是为了给想了解采集的PHPer一个简单的实例,采集其实很简单。。。
源码下载地址:http://www.wangchong.org/index.php/archives/10
[ 本帖最后由 wangchll 于 2007-12-13 19:03 编辑 ] |
|