LoginSignup
99
86

More than 3 years have passed since last update.

EC2 UbuntuでGoogle Chromeをヘッドレス実行してスクリーンショットを採取する手順

Last updated at Posted at 2017-08-09

概要

  • Google Chromeをヘッドレス実行してスクリーンショットを採取する
  • AWS EC2のUbuntuを利用する
  • Python + Selenium + ChromeDriver

手順

コマンドのみを記述していますが、結果も確認する場合は $ commandのようにプロンプト付きで記述します

仮想マシンを起動

  • EC2 より、Ubuntu Server 18.04 LTS (HVM) 64-bit を起動します
  • sshログインして確認
$ cat /etc/issue
Ubuntu 18.04.4 LTS \n \l

作業用ディレクトリ

  • home直下に src とします(適当)
  • 基本的にこのディレクトリをカレントとして作業します
mkdir src
cd src

Google Chromeをインストール

debパッケージをダウンロードしてインストール

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb

依存モジュールをaptでインストール

sudo apt update
sudo apt -f install -y

インストール確認

$ which google-chrome
/usr/bin/google-chrome

Seleniumをインストール

aptでインストール

sudo apt install python3-selenium

ChromeDriverも合わせてインストールされるようです

$ which chromedriver
/usr/bin/chromedriver

日本語フォントをインストール(IPA)

その前に unzip をインストール

sudo apt install unzip

ダウンロードして展開(ファイル名指定する)

wget https://moji.or.jp/wp-content/ipafont/IPAexfont/IPAexfont00401.zip
unzip IPAexfont00401.zip -d ~/.fonts/

フォントキャッシュクリア

fc-cache -fv

fc-cacheコマンドの結果を確認(ユーザホーム部分)

(snip)
/home/ubuntu/.fonts/IPAexfont00401: caching, new cache contents: 2 fonts, 0 dirs
(snip)
fc-cache: succeeded

サンプルソース記述

getss.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('--headless')
options.add_argument('--window-size=1280,1024')

driver = webdriver.Chrome('chromedriver', chrome_options=options)

driver.get('https://ja.wikipedia.org/wiki/Google_Chrome')
driver.save_screenshot('./screenshot.png')
driver.quit()

実行

python3 getss.py

結果:取得したスクリーンショット画像

日本語も正しく表示されることを確認できます(アラビア文字?などは表示できていない)

screenshot.png

99
86
8

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
99
86