php - 在 Controller 中使用 Goutte 和 Symfony2

标签 php symfony web-scraping goutte

我正在尝试抓取一个页面,并且我对php框架不是很熟悉,所以我一直在尝试学习Symfony2。我已经启动并运行了它,现在我正在尝试使用 Goutte。它安装在供应商文件夹中,我有一个用于我的抓取项目的 bundle 。

问题是,从 Controller 进行抓取是一种好的做法吗?如何?我一直在搜索,但无法弄清楚如何从包中使用 Goutte ,因为它深深地埋在文件结构中。

<?php

namespace ontf\scraperBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Goutte\Client;

class ThingController extends Controller
{
  public function somethingAction($something)
  {

    $client = new Client();
    $crawler = $client->request('GET', 'http://www.symfony.com/blog/');
    echo $crawler->text();


    return $this->render('scraperBundle:Thing:index.html.twig');

    // return $this->render('scraperBundle:Thing:index.html.twig', array(
    //     'something' => $something
    //     ));
  }

}

最佳答案

我不确定我是否听说过抓取方面的“良好实践”,但您也许可以在本书 PHP Architect's Guide to Web Scraping with PHP 中找到一些。 .

这些是我在自己的项目中使用的一些准则:

  1. 抓取是一个缓慢的过程,请考虑将该任务委托(delegate)给后台进程。
  2. 后台进程通常作为执行 CLI 应用程序的 cron 作业或持续运行的工作程序运行。
  3. 使用流程控制系统来管理您的员工。看看supervisord
  4. 保存每个抓取的文件(“原始”版本),并记录每个错误。这将使您能够发现问题。使用 Rackspace Cloud Files 或 AWS S3 存档这些文件。
  5. 使用Symfony2 Console tool创建运行爬虫的命令。您可以将命令保存在 Command 目录下的包中。
  6. 使用以下标志运行 Symfony2 命令以防止内存不足: php app/console scraper:run example.com --env=prod --no-debug 其中 app/console 位于Symfony2 控制台应用程序所在的位置, scraper:run 是你的命令的名称, example.com 是一个参数来指示你想要抓取的页面, --env=prod --no-debug 是你应该使用的标志在生产中运行。例如,请参见下面的代码。
  7. 将 Goutte 客户端注入(inject)到您的命令中,如下所示:

Ontf/ScraperBundle/Resources/services.yml

services:
    goutte_client:
        class: Goutte\Client

    scraperCommand:
        class:  Ontf\ScraperBundle\Command\ScraperCommand
        arguments: ["@goutte_client"]
        tags:
            - { name: console.command }

你的命令应该是这样的:

<?php
// Ontf/ScraperBundle/Command/ScraperCommand.php
namespace Ontf\ScraperBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Goutte\Client;

abstract class ScraperCommand extends Command
{
    private $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
        parent::__construct();
    }

    protected function configure()
    {
        ->setName('scraper:run')
            ->setDescription('Run Goutte Scraper.')
            ->addArgument(
                'url',
                InputArgument::REQUIRED,
                'URL you want to scrape.'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output) 
    {
        $url = $input->getArgument('url');
        $crawler = $this->client->request('GET', $url);
        echo $crawler->text();
    }
}

关于php - 在 Controller 中使用 Goutte 和 Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29086862/

相关文章:

php - Symfony 4 : entity has a repositoryClass set to "App\Entity\CommentRepository", 但这不是有效的类

php - 如何在mysql中显示多个表的列名

php - 使用 PHP 将多个 SQL 列合并到 HTML 表的单个列中

Symfony2 : How to restrict/deny access to certain routes by IP address?

python - 如何跳过文件中已存在的行?

javascript - 使用 BeautifulSoup 抓取 JavaScript (ReactTable)

php - 在插入之前插入单引号

php - Symfony2 动态表单域

java - 使用 java (SHA-512) 创建与 symfony2 相同的哈希值

python - 如何在python中抓取td标签内的链接