mcstreetguy / crawler
一个用PHP编写的先进网页爬虫。
Requires
- guzzlehttp/guzzle: ~6.0
- guzzlehttp/psr7: ^1.5
- paquettg/php-html-parser: ^2.0
- ramsey/uuid: ^3.8
- t1gor/robots-txt-parser: ^0.2.4
- webmozart/assert: ^1.4
Requires (Dev)
- kint-php/kint: ^3.2
- phpdocumentor/phpdocumentor: ^2.9
Suggests
- ext-curl: For improved network requests, replaces stream handler usage.
This package is auto-updated.
Last update: 2024-09-06 02:21:15 UTC
README
一个高度可配置的、现代化的PHP网页爬虫。
这个库提供了一个非常动态的环境,用于所有基于递归浏览网页的任务。内部,使用Guzzle发送请求,使用paquettg的HTML解析器搜索服务器响应中的后续链接。
爬虫过程的其余部分完全取决于您。除了爬虫的初始配置外,这个库完全依赖于用户定义的类,例如,检查是否应该爬取链接或处理服务器的响应。这些类仅通过接口进行了非常粗略的预定义,但通常只需要一个函数来调用它们。爬虫不关心这些类的内部工作方式,对于处理器,它甚至不需要任何返回值。
由于这种设计,这个库应该能够几乎无缝地集成到大多数框架中。
目录
安装
通过Composer安装库
$ composer require mcstreetguy/crawler
入门指南
注意: 此示例仅涵盖启动所需的基本要求。如果您想了解更多信息,请查看完整文档。
首先,您需要一个爬虫类的实例。这是库的“根节点”,因为大部分交互都通过这个类的对象进行。
$crawler = new \MCStreetguy\Crawler\Crawler();
这已经足够开始爬取网页,但爬虫目前不会做任何事情。我们还需要一个处理器来处理接收到的响应。
处理器
相应的接口并不复杂,只定义了一个方法。现在我们创建一个简单的例子,它只是简单地输出找到的每个URL
use MCStreetguy\Crawler\Processing\ProcessorInterface; class DebugProcessor implements ProcessorInterface { public function invoke(\MCStreetguy\Crawler\Result\CrawlResult $result) { echo 'Crawled: ' . $result->getUri() . PHP_EOL; } }
invoke
方法接收一个CrawlResult
实例,其中包含有关爬取页面的多个数据。例如,它的uri、服务器响应和在该页面上找到的进一步链接。
现在,如果我们将我们新创建的类的对象添加到爬虫中,我们就可以对某些URL执行它了
$crawler = new \MCStreetguy\Crawler\Crawler(); $crawler->addProcessor(new DebugProcessor); $crawler->execute('http://example.com/');
测试
将上面的部分复制到脚本文件中,并在命令行上执行它,现在会生成以下输出
machine:~ testuser$ php test.php
爬取:http://example.com/
爬取:http://www.iana.org/domains/example
爬取:http://www.iana.org/_css/2015.1/screen.css
爬取:http://www.iana.org/_css/2015.1/print.css
爬取:http://www.iana.org/_img/bookmark_icon.ico
爬取:http://www.iana.org/
爬取:http://www.iana.org/domains
爬取:http://www.iana.org/numbers
^C
machine:~ testuser$
等等!为什么这竟然能工作?
嗯,example.com
实际上是一个现有的网站,并且它恰好包含一个链接。这个链接直接指向互联网数字分配机构(IANA),解释了示例页面的用途。所以我们可以肯定地说,我们的小型测试成功了,爬虫工作正常,因为它到达了example.com
并且找到了其中的链接。但这真的是有意为之的行为吗?不一定,但我们也有解决方案。
验证
为了防止我们的爬虫愉快地在网页间跳转并发现整个互联网,我们需要另一个自定义类:验证器。验证器的工作方式几乎与处理器相同,但它会在处理循环的早期被调用。它接收作为参数传递的待处理URI,并期望返回一个布尔值,指示是否应该爬取该URI。
您可以定义任意多的验证器,这样就可以将复杂的决策拆分成几个部分,但请记住,这就像一个黑名单(即如果某个验证器返回false,则立即丢弃该URI)。
use MCStreetguy\Crawler\Processing\Validation\ValidatorInterface; class DebugValidator implements ValidatorInterface { protected $baseUri; public function __construct(string $baseUri) { $this->baseUri = $baseUri; } public function isValid(\Psr\Http\Message\UriInterface $target) { return (substr_compare((string) $target, $this->baseUri, 0, strlen($this->baseUri)) === 0); } }
如果我们现在在调用爬虫之前添加这个验证器,它应该在处理完第一页后立即停止,因为该页面的链接指向另一个域名。
$crawler->addValidator(new DebugValidator('http://example.com/'));
machine:~ testuser$ php test.php
爬取:http://example.com/
machine:~ testuser$
总结
到此为止,这个库的基本用法应该很清楚。请仔细阅读文档、源代码和Tests/
文件夹,以获取更多信息和一些更高级的示例。
参考
配置
爬虫可以通过配置类进行配置。
(待编写)
验证器
此包包含几个预定义的验证器,这些验证器经常被使用。
RobotsTxtValidator
此验证器从要访问的服务器加载robots.txt
,并将接收到的URI与这些限制进行匹配。只有在这些限制禁止访问URI时,它才会返回false。如果无法加载robots.txt
文件,则所有URI都视为有效。
有关robots.txt
文件的更多信息,请参阅http://www.robotstxt.org/。
用法
此验证器不应直接使用。相反,通过在您的配置中设置$ignoreRobots
属性来启用它。
(有关更多信息,请参阅上面的部分)
DomainWhitelistValidator
此验证器仅允许与用于启动爬取的基URI完全相同的域的URI。
示例
如果http://www.example.com/
是我们的基URI用于爬取,以下URI将被视为有效
http://www.example.com/some-page.html
http://www.example.com/assets/img/my-cool-image.jpg
https://www.example.com/
https://www.example.com/#section
https://www.example.com/?q=somequery
与前面的示例相反,以下将被视为无效
http://example.com/
http://subdomain.example.com/
http://www.google.com/
用法
要使用此验证器,创建其实例并将其添加到爬虫(如先前的DomainWhitelistValidator
)
use MCStreetguy\Crawler\Processing\Validation\Core\DomainWhitelistValidator; $baseUri = 'http://www.example.com/'; $domainValidator = new DomainWhitelistValidator($baseUri); $crawler->addValidator($domainValidator);
SubDomainWhitelistValidator
此验证器仅允许与用于启动爬取的基URI完全相同的域的URI,或者至少在其子域上。
请注意,大多数URL前面都有的www.
被认为不是域的一部分(与RFC规定相反)。这是由于大多数页面将其用作方案指示符,尽管它实际上是一个子域。如果存在,它会被从基URI中移除,以进行主机比较。
这不会对URI过滤产生减少效果,反而会提高爬虫正确验证所有子域的概率。
示例
如果http://www.example.com/
是我们的基URI用于爬取,以下URI将被视为有效
http://www.example.com/some-page.html
http://www.example.com/assets/img/my-cool-image.jpg
https://www.example.com/
https://www.example.com/#section
https://www.example.com/?q=somequery
https://subdomain.www.example.com/
https://another.subdomain.www.example.com/
https://sub.www.example.com/my/path
与前面的示例相反,以下将被视为无效
http://example.com/
http://subdomain.example.com/
http://www.subdomain.example.com/
http://www.google.com/
用法
要使用此验证器,创建其实例并将其添加到爬虫(与DomainWhitelistValidator
类似)
use MCStreetguy\Crawler\Processing\Validation\Core\SubDomainWhitelistValidator; $baseUri = 'http://www.example.com/'; $subdomainValidator = new SubDomainWhitelistValidator($baseUri); $crawler->addValidator($subdomainValidator);