PR

Ubuntu 24.04 Server にウェブサイトのスクリーンショットを取得する環境を構築する方法

1. はじめに

Ubuntu 24.04 Server にウェブサイトのスクリーンショットを取得する環境を構築する方法を記載します。
使用するツールは、

  • Google Chrome
  • ChromeDriver
  • Xvfb
  • ImageMagick

になります。

2. Google Chrome のインストール

sudo apt update
# Google Chrome のインストール
sudo apt install -y wget
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install -y ./google-chrome-stable_current_amd64.deb
# Chrome のバージョンを確認
google-chrome-stable --version

3. ChromeDriver のインストール

# ChromeDriver のインストール
sudo apt install -y unzip
wget https://storage.googleapis.com/chrome-for-testing-public/<google-chrome-stableのバージョン>/linux64/chromedriver-linux64.zip
unzip chromedriver-linux64.zip -d ~/bin/
# パスを通す
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

4. Xvfb のインストール

# Xvfb のインストール
sudo apt install -y xvfb
# 仮想ディスプレイの起動
Xvfb :1 -screen 0 1024x768x24 &
export DISPLAY=:1
# x11-apps のインストール
sudo apt install -y x11-apps

5. ImageMagick のインストール

# ImageMagick のインストール
sudo apt install -y imagemagick

6. ウェブサイトのスクリーンショットを撮影

DISPLAY=:1; google-chrome-stable
xwd -display :1 -root -out test.xwd
convert test.xwd test.png

7. まとめ

以上で、Ubuntu 24.04 Server にウェブサイトのスクリーンショットを取得する環境を構築する方法を記載しました。
注意する点として、Google Chrome のバージョンと ChromeDriver のバージョンを合わせる必要があることです。
これで、CLI環境でウェブサイトが表示できるようになったので、スクレイピングに活用できます。

A. 参考サイト

UbuntuのCLI環境でGoogleChromeを使ってウェブサイトのスクリーンショットを取得する
Chrome for Testing availability
サーバーでWeb画面のスクリーンショットを取る

B. 参考書籍

コメント