6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Colab】永久保存版 Selenium環境の構築(Chrome for Testing を使用)

Last updated at Posted at 2024-09-24

背景

現在のColab環境では、「!pip install selenium」 を実行するだけでスクレイピングの環境構築が完了します(chromeやchromedriverの導入不要)

「以前はちゃんと動いていたのに、、、」

Google Colaboratory で久しぶりに Selenium を使ってスクレイピングしようとするたびに、エラーに見舞われる経験があります。
ネットや生成AIで方法を調べても、良い方法が見つかりません。

そこで試行錯誤した結果、「シンプルな導入コマンド」かつ「バージョンの更新に影響を受けない」ベストな方法を確立したので、備忘録として残します。(急いでいる方はColabへ)

エラーの原因

Seleniumでスクレイピングするためには、一般的に

・google-chrome
・chromedriver

の2つを準備する必要がありますが、両者のバージョンに互換性が無いととエラーになります。

Chrome for Testing

Chrome for Testing は、開発者のためにChromeのバージョンを固定した"chrome"と"chromedriver"のインストーラ(zipファイル)を提供しています。

image.png

Selenium 導入コマンド

Chrome for Testing のウェブサイトを確認し、提供されている Version を指定して、
"google-chrome"と"chromedriver"をColab環境にダウンロード(wget)します。
展開(unzip)したものをパスが通っているディレクトリ(cp)に移動すれば構築完了です。

# seleniumのインストール
!pip install selenium

# Chrome for Testing Version 確認
# https://googlechromelabs.github.io/chrome-for-testing/
# → Stable: 129.0.6668.58

# google-chromeのダウンロード・展開
!wget https://storage.googleapis.com/chrome-for-testing-public/129.0.6668.58/linux64/chrome-linux64.zip -N -P /tmp
!unzip -o /tmp/chrome-linux64.zip -d /tmp/
!rm -rf /tmp/chrome-linux64.zip
!cp -rf /tmp/chrome-linux64 /usr/local/bin/chrome-linux64
!rm -rf /tmp/chrome-linux64
!sudo ln -s /usr/local/bin/chrome-linux64/chrome /usr/local/bin/google-chrome

# chromedriverのダウンロード・展開
!wget https://storage.googleapis.com/chrome-for-testing-public/129.0.6668.58/linux64/chromedriver-linux64.zip -N -P /tmp
!unzip -o /tmp/chromedriver-linux64.zip -d /tmp/
!rm -rf /tmp/chromedriver-linux64.zip
!cp -rf /tmp/chromedriver-linux64/chromedriver /usr/local/bin/
!rm -rf /tmp/chromedriver-linux64

# バージョン確認
!google-chrome --version
!chromedriver --version

本記事の執筆時点では Stable版の最新Versionが「129.0.6668.58」のため、wgetコマンドの2箇所に反映しています。

Selenium サンプルコード

Colabではヘッドレスブラウザの使用が必須のため、Opsionsで設定する必要があります。

# selenium スクレイピングサンプルコード

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=options)

driver.get("https://www.selenium.dev/ja/documentation/")
title = driver.title
driver.close()

print(title)

まとめ

ColabでSeleniumによるスクレイピングを行うための方法を紹介しました。
ChromeのVersionを指定して導入しているため、月日が経って更新がされていても、同じ環境が担保されます。

自動的に最新バージョンを使用したい場合は、Chrome for Testing をスクレイピングして最新のVersionを取得するコードを書くと良いかもしれませんね。

以下に本記事で紹介したコードのColabノートブックを公開しています。

参考リンク

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?