baqend/spider

URL 爬虫,可爬取页面及其所有子页面

1.0.0 2018-03-16 18:08 UTC

This package is not auto-updated.

Last update: 2024-09-22 12:04:52 UTC


README

URL 爬虫,可爬取页面及其所有子页面

安装

请确保已安装 Composer。然后执行

composer require baqend/spider

此包需要至少 PHP 5.5.9 且没有包依赖!

使用

入口点是 Spider 类。为了使其工作,它需要以下服务

  • 队列:收集要处理的 URL。此包包含广度优先和深度优先实现。
  • URL 处理器:检查是否应该处理 URL。如果没有提供 URL 处理器,则处理所有 URL。有关 URL 处理器的更多信息,请参阅此处
  • 下载器:接收 URL 并下载它们。为了不依赖于像 Guzzle 这样的 HTTP 客户端库,您必须自己实现此类。
  • 处理器:检索下载的资产并对其实施操作。有关处理器的更多信息,请参阅此处

您可以使用以下方式初始化爬虫

<?php
use Baqend\Component\Spider\Processor;
use Baqend\Component\Spider\Queue\BreadthQueue;
use Baqend\Component\Spider\Spider;
use Baqend\Component\Spider\UrlHandler\BlacklistUrlHandler;

// Use the breadth-first queue
$queue = new BreadthQueue();

// Implement the DownloaderInterface
$downloader /* your downloader implementation */;

// Create a URL handler, e.g. the provided blacklist URL handler
$urlHandler = new BlacklistUrlHandler(['**.php']);

// Create some processors which will be executed after another
// More details on the processors below!
$processor = new Processor\Processor();
$processor->addProcessor(new Processor\UrlRewriteProcessor('https://example.org', 'https://example.com/archive'));
$processor->addProcessor($cssProcessor = new Processor\CssProcessor());
$processor->addProcessor(new Processor\HtmlProcessor($cssProcessor));
$processor->addProcessor(new Processor\ReplaceProcessor('https://example.org', 'https://example.com/archive'));
$processor->addProcessor(new Processor\StoreProcessor('https://example.com/archive', '/tmp/output'));

// Create the spider instance
$spider = new Spider($queue, $downloader, $urlHandler, $processor);

// Enqueue some URLs
$spider->queue('https://example.org/index.html');
$spider->queue('https://example.org/news/other-landingpage.html');

// Execute the crawling
$spider->crawl();

处理器

此包包含以下内置处理器。

处理器

这是一个聚合处理器,允许添加和删除它将依次执行的其它处理器。

<?php
use Baqend\Component\Spider\Processor\Processor;

$processor = new Processor();
$processor->addProcessor($firstProcessor);
$processor->addProcessor($secondProcessor);
$processor->addProcessor($thirdProcessor);

// This will call `process` on $firstProcessor, $secondProcessor, and finally on $thirdProcessor:
$processor->process($asset, $queue);

HtmlProcessor

此处理器可以处理 HTML 资产并将包含的 URL 入队。它还会修改所有相对 URL 并使它们成为绝对 URL。此外,如果您提供了 CssProcessor,则找到 style 属性,并解析 CSS 中的 URL。

CssProcessor

此处理器可以处理 CSS 资产并将从 @importurl(...) 语句中获取的包含的 URL 入队。

ReplaceProcessor

对资产内容执行简单的 str_replace 操作

<?php
use Baqend\Component\Spider\Processor\ReplaceProcessor;

$processor = new ReplaceProcessor('Hello World', 'Hallo Welt');

// This will replace all occurrences of
// "Hello World" in the asset with "Hallo Welt":
$processor->process($asset, $queue);

ReplaceProcessor 不会入队其他 URL。

StoreProcessor

接收一个 URL 前缀 和一个 目录,并将所有相对于 前缀 的资产存储在 目录 中的相应文件结构中。

StoreProcessor 不会入队其他 URL。

UrlRewriteProcessor

将资产的 URL 更改为另一个前缀。使用此功能使 HtmlProcessorCssProcessor 从不同的来源解析相对 URL。

UrlRewriteProcessor 不会入队其他 URL。此外,它不会修改资产的内容——只有其 URL。

URL 处理器

URL 处理器告诉爬虫是否下载和处理 URL。以下是一些内置的 URL 处理器

OriginUrlHandler

仅处理来自某个给定来源的 URL,例如 "https://example.org"。

BlacklistUrlHandler

不处理属于某些黑名单的 URL。您可以使用 glob 模式提供黑名单

<?php
use Baqend\Component\Spider\UrlHandler\BlacklistUrlHandler;

$blacklist = [
    'https://other.org/**',     // Don't handle anything from other.org over HTTPS    
    'http{,s}://other.org/**',  // Don't handle anything from other.org over HTTP or HTTPS    
    '**.{png,gif,jpg,jpeg}',    // Don't handle any image files    
];

$urlHandler = new BlacklistUrlHandler($blacklist);

替代方案

如果此项目不符合您的需求,请检查以下其他项目