Pythonの簡単な利用方法、GoogleColaboratory googleアカウントで使え、Seleniumでスクレイピングも(ChromiumはDebianから)

Google ColaboratoryでSelenium Chromeスクレイピングの環境構築 google

2023年1月25日時点でWebDriver周りでエラーが出たので解決。

自分はDockerでpythonコンテナ立ち上げて使っていますが、先日プログラムやってみたいという友人に聞かれ、簡単な方法を探していたらGoogleColaboratoryがいいと思ったところ。

自分もそうだったけど、環境構築が面倒なんですよね・・・とりあえず書いて動かしてみたいのに、その環境作るのが面倒で挫折・・・ということも。

スポンサーリンク

Googleアカウントがあればブラウザでアクセスするだけ

下記にアクセスします。

Google Colaboratory

ノートブックを新規作成」をクリックます。

あとはコードを書いていくだけです。

Selenium使ってyahoo開きニュースのリストを取得する

hello worldとか意味なさすぎなので、自分含めた高齢者が大好きなyahooトップのニュースリストを取得するサンプルを走らせてみたいと思います。

とりあえず書いて実行すると、結果が出てくる

自分もpython初心者なので非常に有難いですね。下記の2行を書いて実行すると

from selenium import webdriver
from time import sleep

エラーが出てきます。seleniumないですよね普通。

それにしても環境構築しなくてもいいのは楽。

seleniumとchrome webdriverを入れる

seleniumとchrome webdriverを入れなければなりません。
下記でchrome webdriverとseleniumを先頭行に挿入し、from selenium import webdriver書いて実行すると・・・

!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium

from selenium import webdriver
from time import sleep

順調に進んでいき、エラーなく終わります。その都度気軽にデバッグできるのは便利。

chromeはヘッドレスで

Chromeのwebdriveerオプション設定ではヘッドレスなど指定します。

options = webdriver.ChromeOptions()

options.add_argument('--headless')
options.add_argument('--no-sandbox')

driver = webdriver.Chrome('chromedriver',options=options)

driver.implicitly_wait(10)


url="https://yahoo.co.jp"

driver.get(url)
time.sleep(5)

print("タイトル:", driver.title)

っと、これだとwebdriver初期化(webdriver.Chrome(…))時にエラーが出てきます。
2023/01/25に試しているのですが、colabo内のUbuntu環境だかが変わったようです。

Ubuntu20.04 LTSだとスナップ版以外提供されなくなり、Chrome webdriverが初期化できないので、Debianから引っ張ってこなければならないらしい

ウェブ上でも同様の問題で質問している人がいましたが、解決策があるようです。
Ubuntu20.04 LTSからはChromium スナップ版以外提供されないとかで、chromiumとchrome-webdriverの違いでエラーが出るようになったのか。
下記に解決策が出ており、debian busterレポジトリからchromiumを引っ張り、それをpip installして使う方法。

Google Colab WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 · Issue #10 · kaliiiiiiiiii/Selenium-Profiles
Yust checked Selenium with chome using Selenium-Profiles with this script:

githubでのmetrizable氏のコードで動きます(解決方法)

最初のコードセルに下記をコピペ。

%%shell
# Ubuntu no longer distributes chromium-browser outside of snap
#
# Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap

# Add debian buster
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF

# Add keys
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg

# Prefer debian repo for chromium* packages only
# Note the double-blank lines between entries
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500


Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300


Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF

# Install chromium and chromium-driver
apt-get update
apt-get install chromium chromium-driver

# Install selenium
pip install selenium

次のセルにpythonコードを書く

コードセルを追加し、あとは普通にseleniumを使うコードを書くだけです。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = "https://yahoo.co.jp/"
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome("/usr/bin/chromedriver", options=options)
driver.get(url)
print(driver.title)
driver.quit()

動いてYahoo! JAPANと出てきました。

参考

Issues when trying to use Chromedriver in Colab · Issue #3347 · googlecolab/colabtools
I have been running a program for months that uses Selenium in Google Colab. I have not had an issue with it until tonight. Each time I try to run the webdriver...
Google Colab/Drive に pip インストール: これなら消えない😃
Google Colabにはpythonのpackageを簡単にインストールできますが、一定の条件でリセット、せっかくインストールしたpackageが消えてしまいます。毎回イントールし直すのも面倒なので、Google Driveにpackageをpip installして使うことにしました。これなら消えない☺️
Google Colabで必要モジュールをまとめて入れる方法3選 | DevelopersIO
Google Colab上でpip installで複数必要なモジュールをまとめて行なう方法を3つご紹介します。

コメント

タイトルとURLをコピーしました