exfriend / cloudfactory

一个强大的爬虫库

2.0.3 2016-09-07 13:56 UTC

This package is auto-updated.

Last update: 2024-09-21 20:10:37 UTC


README

logo

CloudFactory 是一个用于在 PHP 中构建爬虫的很好且强大的库。它提供了一个简单、易读的 API,同时保留了底层 cURL 的所有功能。CloudFactory 的关键特性是请求验证。在使用多线程环境中的代理时,你经常会遇到很多问题,如超时、响应欺骗、HTTP/代理错误和无效内容。

CloudFactory 的目标是最大限度地减少 100% 有效的内容交付所需的工作量,你可以通过使用这个库来实现这一点。

###需求

  • PHP 7.0
  • php-curl
  • mbstring
  • guzzle

安装

composer require exfriend/cloudfactory

基本用法

<?php

require 'vendor/autoload.php';

$engine = new \Exfriend\CloudFactory\Engine();
$request = new \Exfriend\CloudFactory\Request('http://httpbin.org/get');
$engine->run($request);

print_r($request->response);

特性

设置 curl 选项

$request = (new \Exfriend\CloudFactory\Request('http://httpbin.org/get'))->setOpt(CURLOPT_FOLLOWLOCATION, true);

选项设置器

您可以直接设置原始 curl 选项,也可以使用在 Request 对象上可用的某些助手。

$request = (new \Exfriend\CloudFactory\Request('http://httpbin.org/post'))
           ->setOpt(CURLOPT_FOLLOWLOCATION, true)
           ->withProxy('127.0.0.1:8080', CURLPROXY_SOCKS5)
           ->sendHeaders(['Foo' => 'Bar'])
           ->post(['user' => 'admin', 'pass' => 'secret'])
           ->withTimeouts(10, 5)
           ->withSsl();

您几乎可以直接在 Engine 实例上设置所有选项。

$engine = new Engine()->withUserAgent()->withSsl()->withCookies('cookie.txt');

并发请求

$engine = (new \Exfriend\CloudFactory\Engine())->setThreads(25);

for ($i = 0; $i < 100; $i++) {
    $request = new \Exfriend\CloudFactory\Request('http://httpbin.org/get?i=' . $i);
    $engine->addRequest($request);
}

$engine->run();

foreach ($engine->requests->processed() as $request) {
    print_r($request->response);
}

请求优先级

您可以通过使用 ->withPriority(int) 来设置每个请求的优先级。这样,当从队列中提取请求时,它们将按优先级排序。

您设置的优先级越高,您的请求在队列中的位置就越靠前。

请求回调

$request = (new Request('http://httpbin.org/get'))
    ->maxTries(3)
    ->store('i', $i)// to pass through to the callback
    ->validateUsing(function (Request $request) { // using closure
        // must return boolean
        return strpos($request->response, 'origin') !== false;
    })
    ->onSuccess('request_success')// using string
    ->onSuccess(function ($r) {
        echo 'You can stack callbacks of one type' . PHP_EOL;
    })
    ->onFail(function ($r) {
        echo "Request failed {$r->tries_current} times of {$r->tries_max}: " . $r->url;
    })
    ->onLastFail(function ($r) {
        echo "Request failed last time: " . $r->url;
    });

响应验证

在构建请求时,您可以使用 validateWith( callable ) 来验证您的响应。您的可调用参数应接受 Request 的实例作为参数,并返回布尔值 true 或 false 作为有效性指示符。

根据有效性,引擎将调用通过 onSuccessonFailonLastFail 提供的回调。

您可以设置 maxTries( int ) 来告知引擎在最后失败之前请求应重复多少次。

请注意,在最后一次失败尝试中,将调用 onFail 和 onLastFail 回调组。

传递额外数据

在创建请求时,您可以使用 store(key,value) 方法来传递任何额外的数据到请求的存储包。您可以从回调中这样访问它

...
  ->store( 'user', $user )
  ->onSuccess(function($request)){
      $user = $request->storage->get('user');
  })
...

动态添加请求

您可以在引擎仍在运行时添加新请求,例如从回调中。

   ->onSuccess(function($r)use($engine){
       $newRequest = new Request('http://site.com/simething_else')
                        ->withSsl()
                        ->onSuccess('some_other_callback');
       $engine->addRequest($newRequest);
   })

请求集合

当您使用 $engine->addRequest() 添加请求时,它会被推送到 $engine->requests 集合。它扩展了 Illuminate\Support\Collection,这是 Laravel 中使用的强大集合模式实现。这意味着您拥有所有的好处,例如使用 $htmls = $engine->requests()->processed()->pluck('response') 来获取所有响应作为数组。

有关集合的完整文档可以在 官方 Laravel 文档 页面上找到。

回调堆叠

您可以使用 $engine->requests 集合或/和回调来处理结果。

您可以针对每个状态添加多个回调。

   ...
     ->onSuccess('parse_content_and_save_it_to_db')
     ->onSuccess('notify_other_class')
     ->onFail('show_notification')
     ->onFail('change_proxy')
     ->onLastFail([$this,'send_some_email'])
   ...

这为您提供了编写代理轮换特质等功能的可能性,该特质是 Request 类的子类。

示例

有关更多示例,请查看 ./examples 目录。

###贡献

此软件包是进行中的工作。