LoginSignup
21
19

More than 5 years have passed since last update.

Dockerでheadless-chromeを使ったスクレイピング環境を整える

Last updated at Posted at 2018-12-13

結論

Dockerfileは以下。

FROM ubuntu:16.04

# Install.
RUN \
  apt-get update && \
  apt-get install -y python3 wget curl unzip apt-utils && \
  curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
  python3 get-pip.py && \
  pip install --upgrade pip && \
  pip install selenium && \
  pip install beautifulsoup4 && \
  apt-get install -y libfontconfig && \
  mkdir -p /home/root/src && cd $_ && \
  wget -q -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip && \
  unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ && \
  apt-get install -y libappindicator1 fonts-liberation libasound2 libnspr4 libnss3 libxss1 lsb-release xdg-utils && \
  touch /etc/default/google-chrome && \
  wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
  dpkg -i google-chrome-stable_current_amd64.deb && \
  apt-get install -y fonts-migmix

RUN apt-get install -y sudo

# add sudo user
RUN groupadd -g 1000 developer && \
    useradd  -g      developer -G sudo -m -s /bin/bash rotelstift && \
    echo 'rotelstift:password' | chpasswd

RUN echo 'Defaults visiblepw'                >> /etc/sudoers
RUN echo 'rotelstift ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Set japanese
RUN apt-get install -y language-pack-ja-base language-pack-ja
RUN locale-gen ja_JP.UTF-8

# Set environment variables.
ENV LANG ja_JP.UTF-8
ENV PYTHONIOENCODIND utf_8

USER rotelstift

# Define default command.
CMD ["/bin/bash"]

で、立ち上げる時は以下のコマンドで。

docker run --privileged -v "path/to/local:path/to/container" -ti hogehoge

--privileged必須。

ささやかな解説

基本

基本のDockerfileは以下を参考にしました。
スクレイピング用のDockerコンテナをつくる
で、もうPhantomJSはメンテされないので代わりにheadless-chromeを使うことにします。

pipはapt-getを使わないでインストール

apt-getを使ってインストールするとpip3 install --upgrade pipをやった時にpipが壊れるため。

Chromeのインストール方法

【その1】Python で headless-chrome でのスクレイピング(Docker Hub / Google CONTAINER REGISTER 登録編)
headless chromeをPythonのseleniumから動かして引数を考えた (Ubuntu 16.04)
の二つをミックスイン。

curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE

で最新のバージョンを取る方法凄いです。

入れないと怒られるやつ

apt-get install -y libappindicator1 fonts-liberation libasound2 libnspr4 libnss3 libxss1 lsb-release xdg-utils

あたり。

作業用ユーザーを作る

Dockerコンテナ内にsudoユーザを追加する
をまるっと参考に。

日本語化

Dockerにubuntuのコンテナを作成・日本語化する
をがっつり参考に。

サンプルコード

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

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.google.com')
print(driver.title)

これでGoogleと出たら成功です。

参考

スクレイピング用のDockerコンテナをつくる
【その1】Python で headless-chrome でのスクレイピング(Docker Hub / Google CONTAINER REGISTER 登録編)
headless chromeをPythonのseleniumから動かして引数を考えた (Ubuntu 16.04)
Dockerコンテナ内にsudoユーザを追加する
Dockerにubuntuのコンテナを作成・日本語化する

21
19
4

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
21
19