如何强制我的应用程序使用 Goutte 而不是 Symfony?

How to force my app to use Goutte instead of Symfony?

我正在尝试使用 Laravel、Goutte 和 Guzzle 对网页进行改写。我正在尝试将 guzzle 实例传递给 Goutte,但我的网络服务器一直在尝试使用 Symfony\Contracts\HttpClient\HttpClientInterfac。这是我得到的确切错误:

Argument 1 passed to Symfony\Component\BrowserKit\HttpBrowser::__construct() must be an instance of Symfony\Contracts\HttpClient\HttpClientInterface or null, instance of GuzzleHttp\Client given, called in /opt/bitnami/apache/htdocs/app/Http/Controllers/ScrapeController.php on line 52

其中 line 52 指的是这一行:$goutteClient = new Client($guzzleclient);

这是我的 class。我怎样才能强制它使用 Goutte 而不是 Symfony?

将此行更改为:$goutteClient = new \Goutte\Client($guzzleclient); 无法解决问题。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Goutte\Client;
use GuzzleHttp\Cookie;
use GuzzleHttp\Client as GuzzleClient;

class ScrapeController extends Controller
{
    public function index()
    {
        return view(‘index’);
    }
    public function scrape() {
        $url = ‘www.domain.com;
        $domain = ‘www.domain.com’;


        $cookieJar = new \GuzzleHttp\Cookie\CookieJar(true);

        // get the cookie from www.domain.com
        $cookieJar->setCookie(new \GuzzleHttp\Cookie\SetCookie([
            'Domain'  => “www.domain.com”,
            'Name'    => ‘_name_session',
            'Value'   => ‘value’,
            'Discard' => true
        ]));
        $guzzleClient = new \GuzzleHttp\Client([
            'timeout' => 900,
            'verify' => false,
            'cookies' => $cookieJar
        ]);
        $goutteClient = new Client($guzzleClient);

        $crawler = $goutteClient->request('GET', $url);
        $crawler->filter('table')->filter('tr')->each(function ($node) {
            dump($node->text());
        });
    }
}

您不能将 GuzzleClient 传递给它,它不支持接受那个。

错误很明显,告诉您 Goutte\Client 必须 采用 Symfony\Contracts\HttpClient\HttpClientInterfacenull 的实例;你不能给它一个GuzzleHttp\Client

在 Symfony 客户端中处理 Cookie 需要遵循此; https://symfony.com/doc/current/http_client.html#cookies.

这是一个有趣的小观察,Gouette\Client is now simply a thin extension of Symfony\Component\BrowserKit\HttpBrowser,因此您可以基于此将 scrape 函数修改为:

use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Component\HttpClient\HttpClient;

...

public function scrape() {
  $url = 'http://www.example.com/';
  $domain = 'www.example.com';

  $jar = new CookieJar();
  $jar->set(new Cookie('_name_session', 'value', null, null, $domain));
  $client = HttpClient::create([
    'timeout' => 900,
    'verify_peer' => false
  ]);
  $browser = new HttpBrowser($client, null, $jar);

  $crawler = $browser->request('GET', $url);
  $crawler->filter('div')->filter('h1')->each(function ($node) {
    dump($node->text());
  });
}

在您的 composer.json 中,您需要具有类似于以下内容的要求:

"symfony/browser-kit": "^5.3",
"symfony/css-selector": "^5.3",
"symfony/http-client": "^5.3"

但是 fabpot/goutte 无论如何都需要它们,所以除了您已经拥有的库之外,不会再下载任何库。