PHP图书网站采集实例教程
Jun 21, 2016 am 08:57 AM
book
lt
nbsp
在网上看到很多简单的采集教程,尤其是针对图书网站的比较多,但附带实例的并不多,在看了一篇针对八路中文网的抓取分析后,决定针对这个网站,写一个简单的抓取教程,并附带实例。由于俺偷懒,文中很多分析都是来自《利用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("/
采集程序其实并不难做,只要分析清楚流程,然后使用合适的正则来取到你想要的内容就可以了。废话不说了,教程开始:
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("/
(.*)/is",$contents,$Arraytitle);
preg_match_all("/【点击阅读】/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("|
preg_match_all("/【点击阅读】/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("|
(.*) 【分卷阅读】(.*)
|Uis",$list_contents,$Block);
//计算总章节数
$regcount=count($Block[0]);
7.分章节进行抓取:
代码:
//进入章节
for($pageBookNum=0;$pageBookNum unset($Zhang);
unset($list_url);
$Zhang=$Block[1][$pageBookNum];//章节标题
writeStatistic('章节:'.($pageBookNum+1).' '.$Zhang."\r\n",$txt_name);//章节标题写入文本文件
preg_match_all("|(.*)|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("/
//计算总章节数
$regcount=count($Block[0]);
7.分章节进行抓取:
代码:
//进入章节
for($pageBookNum=0;$pageBookNum unset($Zhang);
unset($list_url);
$Zhang=$Block[1][$pageBookNum];//章节标题
writeStatistic('章节:'.($pageBookNum+1).' '.$Zhang."\r\n",$txt_name);//章节标题写入文本文件
preg_match_all("|
//进入页面
for($ListNum=0;$ListNum
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("/
(.*)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari?
