LoginSignup
5
5

Laravelのスクレイピング神Goutteがdeprecatedになってた

Posted at

久しぶりにスクレイピングが必要になって、「PHPでスクレイピングといえば」なGoutteを使おうとしたらDeprecatedになっていました。

image.png

今年、2023年4月1日にアーカイブ化されていたみたい。

image.png

対応方法

知りたい方のために、先に対応方法だけ。READMEに書いてある通りです。

WARNING: This library is deprecated. As of v4, Goutte became a simple proxy to the HttpBrowser class from the Symfony BrowserKit component. To migrate, replace Goutte\Client by Symfony\Component\BrowserKit\HttpBrowser in your code.

Google訳「v4 の時点で、Goutte は Symfony BrowserKit コンポーネントから HttpBrowser クラスへの単純なプロキシになりました。移行するには、コード内の Goutte\Client を Symfony\Component\BrowserKit\HttpBrowser に置き換えます。」

プロジェクト内で Goutte\Client というキーワードを Symfony\Component\BrowserKit\HttpBrowser に置換するだけでOKです。

新規プロジェクトの場合

上記は既存プロジェクトでGoutteを既に使っている場合なので、新規で使う場合はcomposerで必要なパッケージをインストールします。自分の場合は symfony/http-client, symfony/browser-kit の2つでいけました。

composer require symfony/http-client
composer require symfony/browser-kit

Goutteのcomposer.jsonが以下のrequireなので、足りないものがあれば追加でrequireしてください。

composer.json
{
    "require": {
        "php": ">=7.1.3",
        "symfony/deprecation-contracts": "^2.1|^3",
        "symfony/browser-kit": "^4.4|^5.0|^6.0",
        "symfony/css-selector": "^4.4|^5.0|^6.0",
        "symfony/dom-crawler": "^4.4|^5.0|^6.0",
        "symfony/http-client": "^4.4|^5.0|^6.0",
        "symfony/mime": "^4.4|^5.0|^6.0"
    },
    "require-dev": {
        "symfony/phpunit-bridge": "^6.0"
    },
}

メモ

deprecated

Goutte/Client.phpを見ると、確かにtrigger_deprecationして、HttpBrowserのコンストラクタを呼んでいるだけ‥!

Goutte/Client.php
class Client extends HttpBrowser
{
    public function __construct(HttpClientInterface $client = null, History $history = null, CookieJar $cookieJar = null)
    {
        trigger_deprecation('fabpot/goutte', '4.0', 'The "%s" class is deprecated, use "%s" instead.', __CLASS__, HttpBrowser::class);

        parent::__construct($client, $history, $cookieJar);
    }
}

HttpClientへの切り替え

そもそも2019年12月にHttpClientへの切り替えが行われています。まったく知らずに使っていました。

感謝

PHPでスクレイピングするにあたって、Goutteには非常にお世話になりました。
感謝。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5