LoginSignup
4
7

More than 5 years have passed since last update.

Python + Chrome on CentOS7(Docker)でスクレイピング

Last updated at Posted at 2018-10-31

概要

Pythonとchromeを使って、CentOS7上でスクレイピングをする環境を構築します。
Dockerで環境を構築します。

Golang版は、こちらです。
Golang + Chrome on CentOS7(Docker)でスクレイピング

関連記事

環境構築

以下の内容はGitHubにpushしてあるので、こちらをcloneしていただいてDockerを起動させれば実現できます。
https://github.com/Esfahan/docker-python-chrome

ディレクトリ構成

以下のディレクトリ構成を前提とします。
/codeは、dockerコンテナにマウントさせる領域です。

code/
Dockerfile
files/

google-chrome.repo

google-chromeを取得するためのレポジトリファイルを作成します。
files/etc/yum.repos.d/google-chrome.repoとして作成します。

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

Dockerfileを作成

From centos:7

ARG PY_VER=3.7.1
ARG CHROME_DRIVER_VER=2.43

RUN yum update -y

##################
# Python, pip
##################
# Dependencies
RUN yum -y install zlib \
                   tk-devel \
                   tcl-devel \
                   ncurses-devel \
                   gdbm-devel \
                   db4-devel \
                   readline-devel \
                   zlib-devel \
                   bzip2-devel \
                   sqlite-devel \
                   openssl-devel \
                   libXext.x86_64 \
                   libSM.x86_64 \
                   libXrender.x86_64 \
                   gcc \
                   gcc-c++ \
                   libffi-devel \
                   python-devel \
                   patch \
                   bzip2 \
                   make

# python
RUN curl -O https://www.python.org/ftp/python/${PY_VER}/Python-${PY_VER}.tgz
RUN tar zxfv Python-${PY_VER}.tgz

WORKDIR Python-${PY_VER}
RUN ./configure --enable-unicode=ucs4 --prefix=/usr/local
RUN make
RUN make install

# pip
RUN curl -O https://bootstrap.pypa.io/get-pip.py
RUN python get-pip.py

# symlink for python3.x
RUN  PY_MVER=$(echo ${PY_VER} | sed s/\.[0-9,]\.[0-9,]*$//g); \
echo ${PY_MVER}; \
if [ "${PY_MVER}" = "3" ]; then \
      ln -s /usr/local/bin/python3 /usr/local/bin/python; \
      ln -s /usr/local/bin/pip3 /usr/local/bin/pip; \
fi

# pip modules
RUN pip install --upgrade pip
RUN pip install selenium

########################
# chromedriver, chrome
########################
# chromedriver
RUN yum install -y unzip
RUN curl -O https://chromedriver.storage.googleapis.com/${CHROME_DRIVER_VER}/chromedriver_linux64.zip
RUN unzip chromedriver_linux64.zip
RUN mv chromedriver /usr/local/bin/

# Dependencies
RUN yum install -y libX11 \
                   GConf2 \
                   fontconfig

# chrome
ADD files/etc/yum.repos.d/google-chrome.repo /etc/yum.repos.d/google-chrome.repo
RUN yum install -y google-chrome-stable \
                   libOSMesa

# fonts
RUN yum install -y google-noto-cjk-fonts \
                   ipa-gothic-fonts

build

$ docker build -t python-chrome:latest ./

コンテナ起動

$ docker run --name python-chrome-container \
             -v $(pwd)/code:/code \
             -itd python-chrome:latest /bin/bash

Pythonのスクリプトを作成

code/main.pyとして作成します。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
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('--disable-gpu')
options.add_argument('--window-size=1280,1024')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(options=options)

driver.get('https://www.google.co.jp/')
print(driver.title) #=> Google
driver.save_screenshot('Google.png')

実行

これを実行すると、GoogleのトップページのtitleであるGoogleが表示され、Google.pngとして、スクリーンショットが作成されます。

$ docker exec -it python-chrome-container /bin/bash -c 'python /code/main.py'
Google

Prevent Error: Fontconfig warning: "/etc/fonts/fonts.conf", line 86: unknown element "blank"というエラーが出る場合は、/etc/fonts/fonts.confの、<blank></blank>の削除、もしくはコメントアウトをして下さい。

参考:発生したエラーについて

参考

4
7
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
4
7