利用PHP制作简单的内容采集器的原理分析
前几天做了个小说连载的程序,因为怕更新麻烦,顺带就写了个采集器,采集八路中文网的,功能比较简单,不能自定义规则,不过大概思路都在里面了,自定义规则可以自己来扩展。
用php来做采集器主要用到两个函数:file_get_contents()和preg_match_all(),前一个是远程读取网页内容的,不过只在php5以上的版本才能用,后一个是正则函数,用来提取需要的内容的。
下面就一步一步来讲功能实现。
因为是采集小说,所以首先要将书名、作者、类型这三个提取出来,别的信息可根据需要提取。
这里以《回到明朝当王爷》为目标,先打开书目页,链接:http://www.86zw.com/Book/3727/Index.aspx
多打开几本书会发现,书名的基本格式是:http://www.86zw.com/Book/书号/Index.aspx,于是我们可以做一个开始页,定义一个,用来输入需要采集的书号,以后就可以通过$_POST[‘number']这种格式来接收需要采集的书号了。接收到书号,下面要做的就是构造书目页:$url=http://www.86zw.com/Book/$_POST[‘number']/Index.aspx,当然这里是举个例子,主要是为了讲解方便,实际制作的时候最好检查一下$_POST[‘number']的合法性。
构造好URL以后就可以开始采集书籍信息了。使用file_get_contents() 函数打开书目页:$content=file_get_contents($url),这样就能将书目页的内容都读取出来了。接下来就是将书名、作者和类型等信息匹配出来了。这里就以书名为例,其他的都一样。 打开书目页,查看源文件,找到“《回到明朝当王爷》”,这就是要提取出来的书名了。提取书名的正则表达式:/(.*?)\/is,使用preg_match_all()函数将书名取出:preg_match_all("/(.*?)\/is",$contents,$title);这样$title[0][0]的内容就是我们要的标题了(preg_match_all函数的用法可以去百度查,这里就不详细说明了)。取出了书籍信息,接下来就是取章节内容了,要取章节内容,首先要做的就是找到每一章的地址,然后远程打开章节,用正则将内容取出来,入库或者直接生成html静态文件。这个是章节列表的地址:http://www.86zw.com/Html/Book/18/3727/List.shtm,可以看出这个和书目页一样,是有规律可寻的:http://www.86zw.com/Html/Book/分类号/书号/List.shtm。书号前面已经取得,这里的关键是找到分类号,分类号可以在前面的书目页找到,提取分类号:
preg_match_all("/Html\/Book\/[0-9]{1,}\/[0-9]{1,}\/List\.shtm/is",$contents,$typeid);这样还不够,还需要一个切取函数:
PHP代码如下:
function cut($string,$start,$end){
$message = explode($start,$string);
$message = explode($end,$message[1]); return $message[0];}其中$string为要被切取的内容,$start为开始的地方,$end为结束的地方。取出分类号:
$start = "Html/Book/";
$end
= "List.shtm";
$typeid = cut($typeid[0][0],$start,$end);
$typeid = explode("/",$typeid);[/php]
这样,$typeid[0]就是我们要找的分类号了。接下来就是构造章节列表的地址了:$chapterurl = http://www.86zw.com/Html/Book/.$typeid[0]/$_POST[‘number']/List.shtm。有了这个就能找到每一章节的地址了。方法如下:
$ustart = "\"";
$uend
= "\"";
//t表示title的缩写
$tstart = ">";
$tend
= "//取路径,例如:123.shtm,2342.shtm,233.shtm
preg_match_all("/\"[0-9]{1,}\.(shtm)\"/is",$chapterurl,$url);
//取标题,例如:第一章 九世善人
preg_match_all("//is",$file,$title);
$count = count($url[0]);
for($i=0;$i{
$u = cut($url[0][$i],$ustart,$uend);
$t = cut($title[0][$i],$tstart,$tend);
$array[$u] = $t;
}
$array数组就是所有的章节地址了,到这里,采集器就完成一半了,剩下的就是循环打开每个章节地址,读取,然后将内容匹配出来。这个比较简单,这里就不详细叙述了。好了,今天就先写到这吧,第一次写这么长的文章,语言组织方面难免有问题,还请大家多包涵!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
