msgbartop
平凡的生活,默默的期待,等待蜕变的到来……
msgbarbottom

12 十二 07 [原创]图书网站采集实例教程

在网上看到很多简单的采集教程,尤其是针对图书网站的比较多,但附带实例的并不多,在看了一篇针对八路中文网的抓取分析后,决定针对这个网站,写一个简单的抓取教程,并附带实例。由于俺偷懒,文中很多分析都是来自《利用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(“/

(.*)<\/span><\/div>/is”,$contents,$Arraytitle);
preg_match_all(“/【点击阅读<\/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(“|
(.*)  【分卷阅读<\/a>】<\/div>(.*)
<\/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("|
  • (.*)<\/a><\/li>|Uis”,$Block[3][$pageBookNum],$list_url);
    //进入页面
    for($ListNum=0;$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("/
    (.*)
  • sidebarbottom
    sidebartop

    分类

    sidebarbottom
    sidebartop

    标签云

    sidebarbottom