LoginSignup
4
3

More than 3 years have passed since last update.

DockerでPuphpeteerを使う

Last updated at Posted at 2020-09-02

この記事について

すでにあるプロジェクトにPuphpeteerを導入する

自分用メモです

作業

in-container-terminal
$ composer require nesk/puphpeteer
$ npm install @nesk/puphpeteer puppeteer

メモリでコケたらphp.iniを修正

php.ini
memory_limit=1G
Dockerfile
FROM php:7.4.1-fpm

# 省略

# puppeteerのインストール
RUN apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils libgbm-dev wget
RUN npm install --global --unsafe-perm puppeteer

# 日本語フォントのインストール(これ入れないと日本語のサイトで文字化けする)
RUN apt-get install fonts-ipafont-gothic fonts-ipafont-mincho

# 省略

以下の処理を実行してexample.pngができていれば成功

puppeteer
<?php

use Illuminate\Console\Command;
use Nesk\Puphpeteer\Puppeteer;

class Scraping
{

    public function main()
    {
        $puppeteer = new Puppeteer;
        $browser = $puppeteer->launch([
            'args' => ['--no-sandbox', '--disable-setuid-sandbox']
        ]);
        $page = $browser->newPage();
        $page->goto('https://example.com');
        $page->screenshot(['path' => 'example.png']);

        $browser->close();
    }
}

参考

4
3
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
4
3