Chromebook Ideapad DuetでSeleniumを動かす

Chromebook Ideapad DuetでSeleniumを動作させるまでのセットアップ内容を備忘録がてら書きます。

なぜやるか

Webスクレイピングをするためです。お試しで使っていたGASでは限界が来たので、もう少し腰を据えて持っている端末からちゃんとやることにします。

ChromebookSeleniumGoogleChromeを操作できないらしい

複雑なログイン操作などがある場合はSeleniumでブラウザ操作を自動化することが有効です。手持ちのChromebookでもやりたいと思い色々調べたところ、一癖あることがわかりました。

詳しくは、先人が解説いただいています。勉強になりますmm nochitamama.hatenablog.com

なにをするか

Chromebook Ideapad DuetでLinuxを立ち上げたまっさらな状態からpipをインストールし、SeleniumでChroniumを操作するところまでセットアップします。

動作環境

  • OS バージョン: 93.0.4577.107
  • Linux仮想環境 Linux penguin 5.4.131

Linux仮想マシン
Chromebookでは **ターミナル** というアプリを起動するだけでLinux仮想マシンが立ち上がります。

手順

初期状態からChromiumインストールまで

# aptアップデート
sudo apt update && sudo apt upgrade -y
Get:1 https://deb.debian.org/debian buster InRelease [122 kB]
Get:2 https://deb.debian.org/debian-security buster/updates InRelease [65.4 kB]                     
Ign:3 https://storage.googleapis.com/cros-packages/93 buster InRelease
 〜略〜
# ※末尾の出力内容を取り逃しました・・・

# chroniumのインストール
$ sudo apt install -y chromium
Reading package lists... Done
Building dependency tree       
Reading state information... Done
 〜中略〜
Setting up chromium (89.0.4389.114-1~deb10u1) ...  # バージョンの表記あり
Processing triggers for desktop-file-utils (0.23-4) ...
Processing triggers for mime-support (3.62) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for libc-bin (2.28-10) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for dbus (1.12.20-0+deb10u1) ...
Processing triggers for fontconfig (2.13.1-2) ...
# 試しにChroniumを起動してみる
$ chromium
[4121:4121:1023/001459.044914:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
[4137:7:1023/001501.231224:ERROR:command_buffer_proxy_impl.cc(122)] ContextResult::kTransientFailure: Failed to send GpuChannelMsg_CreateCommandBuffer.
 〜ブラウザが立ち上がる〜

f:id:uhfuji:20211023155140p:plain
Chromium

Chromiumを入れることができました。ここからは、Seleniumで操作できるためのセットアップが続きます。

Chromium-driverインストール

URLから chromium-driver[バージョン]deb10u1_arm64.deb をダウンロードします。 https://packages.debian.org/ja/buster/arm64/chromium-driver/download

ダウンロードしたファイルをダブルクリックしてインストールを実行します。

# ChromiumDriverの動作確認
$ chromedriver
Starting ChromeDriver 89.0.4389.114 (1ea76e193b4fadb723bfea2a19a66c93a1bc0ca6-refs/branch-heads/4389@{#1616}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.   # successfullyが出ればOK

pipとSeleniumのインストール

まだ続きます。がんばりましょう。Debianにはデフォルトでpipが入っていないそうなので、インストールします。続けてpipでSeleniumのパッケージをインストールします。

# 念のためaptアップデート
$ sudo apt update
Hit:1 https://deb.debian.org/debian buster InRelease
Ign:2 https://storage.googleapis.com/cros-packages/93 buster InRelease
Hit:3 https://storage.googleapis.com/cros-packages/93 buster Release
Get:4 https://deb.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:6 https://deb.debian.org/debian-security buster/updates/main arm64 Packages [303 kB]
Fetched 368 kB in 2s (174 kB/s)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.

# pipをインストール
$ sudo apt install python3-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
 〜中略〜
Setting up python3-dev (3.7.3-1) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.28-10) ...

# seleniumをインストール
$ pip3 install selenium
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/ad/24/39cab5fbaf425ff522e1e51cce79f94f10f9523f015d2b2251e43f45e8a2/selenium-4.0.0-py3-none-any.whl (954kB)
    100% |████████████████████████████████| 962kB 604kB/s 
 〜中略〜
pyopenssl 21.0.0 has requirement cryptography>=3.3, but you'll have cryptography 2.6.1 which is incompatible.
Installing collected packages: pyOpenSSL, certifi, idna, urllib3, async-generator, h11, wsproto, attrs, sortedcontainers, outcome, sniffio, trio, trio-websocket, selenium
Successfully installed async-generator-1.10 attrs-21.2.0 certifi-2021.10.8 h11-0.12.0 idna-3.3 outcome-1.1.0 pyOpenSSL-21.0.0 selenium-4.0.0 sniffio-1.2.0 sortedcontainers-2.4.0 trio-0.19.0 trio-websocket-0.9.2 urllib3-1.26.7 wsproto-1.0.0

PythonSeleniumを動かしてみる

さぁ、あとは動かすだけです!!

from selenium import webdriver
  
driver = webdriver.Chrome()
driver.get('https://google.com/')

Chromiumが起動し、Googleの画面が出れば成功です! f:id:uhfuji:20211023163149p:plain

ようやくWebスクレイピングを行う下準備ができましたので、これから実装していきます。