[PYTHON] Steps to run Google Chrome headless on EC2 Ubuntu and take screenshots

Overview

--Run Google Chrome headless and take screenshots --Using Ubuntu on AWS EC2

procedure

Only the command is written, but if you want to check the result, write it with a prompt like $ command.

Start virtual machine

--From EC2, start ʻUbuntu Server 18.04 LTS (HVM) 64-bit` --ssh Log in and check

$ cat /etc/issue
Ubuntu 18.04.4 LTS \n \l

Working directory

--Set src directly under home (appropriate) --Basically, this directory is the current directory.

mkdir src
cd src

Install Google Chrome

Download and install the deb package

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

Install dependent modules with apt

sudo apt update
sudo apt -f install -y

Installation confirmation

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

Install Selenium

Install with apt

sudo apt install python3-selenium

It seems that Chrome Driver will also be installed

$ which chromedriver
/usr/bin/chromedriver

Install Japanese fonts (IPA)

Install unzip before that

sudo apt install unzip

Download and extract (specify the file name)

wget https://ipafont.ipa.go.jp/IPAexfont/IPAexfont00401.zip
unzip IPAexfont00401.zip -d ~/.fonts/

Font cache clear

fc-cache -fv

Check the result of the fc-cache command (user home part)

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

Sample source description

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()

Run

python3 getss.py

Result: Screenshot image taken

You can confirm that Japanese is also displayed correctly (Arabic characters? Etc. are not displayed)

screenshot.png

Recommended Posts

Steps to run Google Chrome headless on EC2 Ubuntu and take screenshots
Install and run dropbox on Ubuntu 20.04
Preparing to run Flask on EC2
Steps to install Python environment on Ubuntu
[Python] Run Headless Chrome on AWS Lambda
How to run MeCab on Ubuntu 18.04 LTS Python
[Latest] How to use Python library to save Google image search & use Chrome Driver on ubuntu
Install Apache 2.4 on Ubuntu 19.10 Eoan Ermine and run CGI
Steps to measure coverage and get badges on codecov.io
Run the Caffe model on Google Colaboratory to predict the age and gender of the world's supermodels
How to share OS and Vim clipboard on Ubuntu 18.04.3 LTS
Run Jupyter on Ubuntu on Windows
How to install OpenCV on Cloud9 and run it in Python
[EC2] How to install chrome and the contents of each command
Install Ubuntu 18.04 on MacBook Pro Touchbar model and connect to WIFI
How to run Jupyter and Spark on Mac with minimal settings
How to install pandas on EC2 (How to deal with MemoryError and PermissionError)