ahmard/queliwrap

QueryList PHP 网页爬虫包装器。

4.0.4 2022-12-21 15:30 UTC

README

Queliwrap 是一个包装器,它提供了围绕 PHP 流行网页爬虫 QueryListGuzwrap 的简单辅助函数。

注意: Queliwrap\Wrapper\Queliwrap::exec() 方法已被替换为 execute()。现在 exec() 返回符合 psr-7 的对象,而 execute() 返回 QueryList 对象。

安装

请确保您已安装 Composer

composer require ahmard/queliwrap

安装完成后,在您的代码中引入 Composer 的自动加载器

require 'vendor/autoload.php';

用法

Queliwrap 依赖于 Guzwrap,您可能需要深入了解它.

use Queliwrap\Client;

Client::get('https://google.com')->execute()
    ->find('ul')->eq(0)
    ->find('li');

错误处理

use Queliwrap\Client;

try {
    $text = Client::get('https://google.com')->execute()
        ->find('ul')->eq(0)
        ->find('li')
        ->text();
        
    echo $text;
}catch (Throwable $exception){
    echo $exception->getMessage();
}

提交表单

use Guzwrap\Wrapper\Form;
use Queliwrap\Client;

Client::post(function(Form $form){
    $form->action('https://:8080/rand/guzwrap.php');
    $form->field('name', 'Jane Doe');
    $form->file('image', 'C:\1.jpg');
});

Cookie

感谢 Guzwrap,Cookie 可以在多个请求之间保持

use Guzwrap\Wrapper\Form;
use Queliwrap\Client;

//Login
Client::create()
    ->referer('https://:8000')
    ->withSharedCookie()
    ->form(function (Form $form){
        $form->action('https://:8000/login');
        $form->method('POST');
        $form->input('email', 'queliwrap@example.com');
        $form->input('password', 1234);
        $form->input('remember_me', 1);
    })->exec();

//View user profile
$queryList = Client::create()
    ->get('https://:8000/users/view')
    ->query(['id' => 2])
    ->withSharedCookie()
    ->execute();

//Find user info
$firstName = $queryList->find('.profile')
    ->find('list-group-item')
    ->eq(0)
    ->text();

echo "First name: {$firstName}";

文档

许可

Queliwrap 是 MIT 许可证。