ブラックボックス

プログラミングのノウハウやライフハックをどんどん投稿していきたい

Jan 8, 2018 - 2 minute read - ノウハウ

【Docker】Amazon Linux2でHeadless Chromeを使用する

【Docker】Amazon Linux2にChromeをインストールする からの続き。
一部を除いてはCentOS7系とほぼ同じではありますが。

環境:

  • Ubuntu 16.04(Vagrant ゲストOS)
  • Docker 17.09.1-ce
  • Amazon Linux 2.0(2017.12)

まずはPython3を有効にする。

# amazon-linux-extras install python3
  0  ansible2   disabled  [ =2.4.2 ]
  1  emacs   disabled  [ =25.3 ]
  2  memcached1.5   disabled  [ =1.5.1 ]
  3  nginx1.12   disabled  [ =1.12.2 ]
  4  postgresql9.6   disabled  [ =9.6.6 ]
  5  python3=latest  enabled  [ =3.6.2 ]
  6  redis4.0   disabled  [ =4.0.5 ]
  7  R3.4   disabled  [ =3.4.3 ]
  8  rust1   disabled  [ =1.22.1 ]
  9  vim   disabled  [ =8.0 ]
 10  golang1.9   disabled  [ =1.9.2 ]
 11  ruby2.4   disabled  [ =2.4.2 ]
 12  nano   disabled  [ =2.9.1 ]
 13  php7.2   disabled  [ =7.2.0 ]
 14  lamp-mariadb10.2-php7.2   disabled  [ =10.2.10_7.2.0 ]
# python3 -V
Python 3.6.2

PIPで必要なモジュールをインストール

# pip3 install beautifulsoup4
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting beautifulsoup4
  Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB)
    100% |????????????????????????????????| 92kB 2.3MB/s
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.6.0

# pip3 install lxml
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting lxml
  Downloading lxml-4.1.1-cp36-cp36m-manylinux1_x86_64.whl (5.6MB)
    100% |????????????????????????????????| 5.6MB 206kB/s
Installing collected packages: lxml
Successfully installed lxml-4.1.1
# pip3 install selenium
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting selenium
  Downloading selenium-3.8.0-py2.py3-none-any.whl (941kB)
    100% |????????????????????????????????| 942kB 926kB/s
Installing collected packages: selenium
Successfully installed selenium-3.8.0

Chromeドライバを取得

# wget https://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip
--2017-12-26 03:43:07--  https://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip
Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 172.217.25.208, 2404:6800:4004:80d::2010
Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|172.217.25.208|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3730403 (3.6M) [application/zip]
Saving to: 'chromedriver_linux64.zip'

100%[======================================>] 3,730,403   4.81MB/s   in 0.7s

2017-12-26 03:43:09 (4.81 MB/s) - 'chromedriver_linux64.zip' saved [3730403/3730403]
# unzip chromedriver_linux64.zip
Archive:  chromedriver_linux64.zip
  inflating: chromedriver
# mv chromedriver /usr/local/bin/

テストコードを作成

# vi test.py
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

url = 'http://www.google.co.jp/'
try:
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(chrome_options=options)
    driver.get(url)
    time.sleep(2)
    title = driver.title
    html = driver.page_source.encode('utf-8')
    soup = BeautifulSoup(html, 'lxml')
    for s in soup(['script', 'style']):
        s.decompose()

    text = ' '.join(soup.stripped_strings)

    print(title)
    print(text)
except:
    print('error')
finally:
    driver.quit()

コードを実行

# python3 test.py
Google
Google Gmail 画像 アカウント 検索 マップ YouTube Play ニュース Gmail ドライブ カレンダー Google+ 翻訳 フォト もっと見る ショッピング ドキュメント ブックス Blogger 連絡先 ハングアウト Keep Earth さらにもっと ログイン 判別できません。エラーが発生しました。 判別を再開始 ヘルプ 起動ワード検出はオフになっています。 「OK Google」の判別を開始 Google インスタント検索はご利用いただけません。検索するには Enter キーを押してください。 詳細 接続が遅いため Google インスタント検索をオフにしました。検索するには Enter キーを押してください。 検索するには Enter キーを押し ます。 不適切な検索候補の報告 × 日本 プライバシー 規約 設定 検索設定 検索オプション 履歴 ヘルプを検索 フィードバックを送信 広告 ビジネス Googleについて