alpine linux上でpythonでChrome Headlessを操作
2018/04/09
alipine linuxなるものがあるらしく、調べてみるとdocker用にカスタマイズされたlinuxディストリビューションの様です。
コンテナのサイズがubuntuやcentosに比べて非常に小さいので、起動までが非常に早いです。
alpine linuxにHeadless Chromeをインストールし、pythonから操作したいと思います。
コンテナの作成
まずDockerfileを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
FROM python:alpine3.7 RUN apk add --update \ udev \ ttf-freefont \ chromium \ openssl \ chromium-chromedriver \ gfortran \ gcc \ g++ RUN mkdir /noto ADD https://noto-website.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip /noto WORKDIR /noto RUN unzip NotoSansCJKjp-hinted.zip && \ mkdir -p /usr/share/fonts/noto && \ cp *.otf /usr/share/fonts/noto && \ chmod 644 -R /usr/share/fonts/noto/ && \ fc-cache -fv WORKDIR / RUN rm -rf /noto CMD ["python3"] |
Dockerfileを作成したら下記のコマンドを実行し、コンテナに接続。
1 2 3 4 5 6 7 8 |
#Create Image docker build -t chrome:py . #Run container docker run -it -d chrome:py ash #attach container docker attach container |
コンテナに接続後、pythonを実行しスクリーンショットを取得。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> from selenium import webdriver >>> from selenium.webdriver.chrome.options import Options >>> options = Options() >>> options.add_argument('--headless') >>> options.add_argument('--disable-gpu') >>> options.add_argument('--window-size=1280,1024') >>> options.add_argument('--no-sandbox') >>> driver = webdriver.Chrome(chrome_options=options) >>> driver.get('https://www.google.co.jp/') >>> print(driver.title) Google >>> driver.save_screenshot('test.png') True |
test.pngが取得できればOK
ハマったところ
pip install seleniumを実行したときにエラー
1 2 3 4 5 6 7 8 |
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)': /simple/selenium/ Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)': /simple/selenium/ Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)': /simple/selenium/ Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)': /simple/selenium/ Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)': /simple/selenium/ Could not fetch URL https://pypi.python.org/simple/selenium/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /simple/selenium/ (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)) - skipping Could not find a version that satisfies the requirement selenium (from versions: ) No matching distribution found for selenium |
opensslをインストールすることで解決。
1 |
# apk add openssl |
pandas,numpyのインストール時にエラー
pip install numpyコマンド実行時に、すごい量のエラーログが出力されました。
gcc,g++,gfortranをインストールしないとコンパイル時にエラーが起こるようです。。。