survos/scraper-bundle

抓取和缓存网页

支持包维护!
kbond

安装次数: 1,701

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 3

分支: 0

公开问题: 0

类型:symfony-bundle

1.5.340 2024-08-25 15:48 UTC

This package is auto-updated.

Last update: 2024-09-24 13:45:17 UTC


README

一个允许基于磁盘的网页抓取缓存的 Symfony 扩展包。

它还允许从 Twig 进行抓取。虽然这在生产环境中不是最佳实践,但它可以加快原型和演示的速度。

最终这将成为一个真正的缓存适配器,但现阶段只需将网页抓取到本地存储即可。

安装包后,

安装

composer req survos/scraper-bundle

如果您没有使用 Flex,通过将类添加到 bundles.php 中来启用扩展包

// config/bundles.php
<?php

return [
    //...
    Survos\Bundle\SurvosScraperBundle::class => ['all' => true],
    //...
];

工作演示

复制以下内容以查看其工作情况。

symfony new --webapp scraper-bundle-demo && cd scraper-bundle-demo
composer req survos/scraper-bundle
symfony console make:controller AppController
sed -i "s|/app|/|" src/Controller/AppController.php 

cat <<'EOF' > templates/app/index.html.twig
{% extends 'base.html.twig' %}
{% block body %}
    {% set url = 'https://jsonplaceholder.typicode.com/users' %}
    {% set users = request_data(url) %}
    <ul>
        {% for row in users %}
            <li>{{ row.name }} / {{ row.website }}</li>
        {% endfor %}
    </ul>
{% endblock %}
EOF
symfony server:start -d
symfony open:local

当您刷新页面时,它将使用缓存数据并变得更快。要查看调试工具栏中的抓取,请清除缓存并重新加载。

bin/console cache:pool:clear --all
symfony open:local

要在服务或控制器中使用,请注入缓存。

    public function index(ScraperService $scraper): Response
    {
        $data = $scraper->fetchData('https://jsonplaceholder.typicode.com/albums', asData: 'object');
        
    }