LoginSignup
2
4

More than 5 years have passed since last update.

Puppeteer を Ubuntu18.04 で 動かすため snap を使ってみる

Last updated at Posted at 2018-09-04

参考

Linuxディストリビューションを問わず動作するソフトウェアパッケージ「Snap」

インストール

deb系
sudo apt -y install snapd
# 一覧に表示されない場合は以下
systemctl daemon-reload
Fedora
sudo dnf -y install snapd
sudo systemctl enable --now snapd.socket

Node.js 8をインストールしてみる

  • Ubuntu 18.04のaptからインストール出来るnodejsは version 6
    • よくある Puppeteerのサンプルコード を動かすには version 8 が必要
# nodeという名前のコマンドを検索
snap search node

# インストール出来るバージョン一覧を確認
snap info node

# インストール
sudo snap install --channel=8/stable --classic node

Puppeteerをインストール

npm install puppeteer
test.js
const puppeteer = require('puppeteer');

(async () => {
  //
  const browser = await puppeteer.launch();
  // profile指定したい場合
  //const browser = await puppeteer.launch({
  //  userDataDir: 'C:\\usrdata\\puppeteer_crawler_profile'
  //});

  const page = await browser.newPage();
  // googleを表示
  await page.goto('https://google.com');
  // example.pngに画面キャプチャ保存
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();
実行
node test.js

chrome extension

daydream.png

docker

Dockerfile
FROM ubuntu:18.04

RUN apt update
RUN apt install -y \
      curl gnupg chromium-browser fonts-ipafont-gothic
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs
RUN npm install puppeteer

WORKDIR /root
COPY entrypoint.sh .
COPY entrypoint.js .
ENTRYPOINT ["/root/entrypoint.sh"]
entrypoint.sh
#!/bin/sh
echo ${URL:-https://google.co.jp}
echo ${WAIT_SEC:-86400}

while :
do
  node entrypoint.js
  sleep $WAIT_SEC
done
entrypoint.js
const puppeteer = require('puppeteer');

(async () => {
  //
  const browser = await puppeteer.launch({
    args: [
      '--no-sandbox',
    ]
  });
  // profile指定したい場合
  //const browser = await puppeteer.launch({
  //  userDataDir: 'C:\\usrdata\\puppeteer_crawler_profile'
  //});

  const page = await browser.newPage();
  // アクセス
  await page.goto(process.env.URL);
  // example.pngに画面キャプチャ保存
  await page.screenshot({path: '/data/example.png'});

  await browser.close();
})();
run.sh
docker run --rm -it \
 -e URL="http://hogehoge.com" \
 -v $(pwd)/data:/data/ \
 faf1044237d1
2
4
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
2
4