LoginSignup
8
13

More than 5 years have passed since last update.

ブラウザ上の操作をRaspberry Piを使って自動化する方法

Last updated at Posted at 2019-01-12

はじめに

ブラウザ上の定期的に行う処理や繰り返し行う処理を自動でやりたいというモチベーションのもと、それをRaspberry Piにやってもらうというのがこの記事の目的です。要素技術としては、以下の2点です。

  • selenium + chromedriverのRaspberry Pi上での使い方
  • cronの設定

この記事はRaspberry PiをセットアップしてSSH接続し、Python環境ができていることを前提としています。Raspberry Piを一から用意する場合は、ゼロからはじめるRaspberry Pi [OSの書き込み/SSH接続]を見ながら進めて下さい。

環境

  • LEMORE Raspberry pi 3 model b+ Starter Kit
  • OS: raspbian (kernel version 4.14)
  • Python 3.6.8 (pyenvによる)

chrome driverの用意

ブラウザ上の自動実行を行うにはseleniumが便利です。Pythonからも利用できます。seleniumでは通常、実行するとブラウザが起動され勝手にブラウザが操作されますが、headlessバージョンで使用すると、ブラウザを起動せずに利用できます。今回はこのheadlessバージョンを使用します。また、seleniumの利用に際してdriverが必要なのですが、chromeのドライバーを使用します。

以下のページから各OS(Mac, Windows, Linux)に対応したdriverを取得できます。

しかし、今回のraspbianの場合、Linux版でも実行できませんでした。Chromiumドライバを使えば対応可能とのことでこちらを使います。(Mac, Windows, Linuxなら上のURLからdriverをもってくればOKです。)

chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.debをダウンロードしてきます。

$ https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+build/14482955/+files/chromium-codecs-ffmpeg-extra_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb
$ sudo dpkg -i chromium-codecs-ffmpeg-extra_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb
$ ls /usr/lib/chromium-browser/chromedriver # 確認

これでchrome driverの用意は完了です。

seleniumの実行コード

次に、seleniumで自動でブラウザ処理を実行するコードを書きます。以下のコードがテンプレートです。「処理」と書かれたところに実行する内容を記述します。save_screenshotによってエラーが出た場合はスクリーンショットを残してくれます。

# -*- coding: utf-8 -*-

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

options = Options()
options.add_argument('--headless')

driver = webdriver.Chrome(executable_path="/usr/lib/chromium-browser/chromedriver", chrome_options=options)
driver.get(url)

try:
    # 処理
except Exception as e:
    print(e)
    driver.save_screenshot('screenshot.png')

driver.close()

処理の書き方については本記事では扱いませんが、Python + Selenium で Chrome の自動操作を一通りを参考にする等調べてみて下さい。

seleniumがない場合はインストールして下さい。

$ pip install selenium

cronの設定

raspbian OS上でcronの設定を行い、上記の実行ファイルを定期実行させます。

cronのファイルの用意

まず、cronのテンプレートをコピーします。コピーしたファイルの自分で設定したい内容を記述します。

$ cp /etc/crontab /etc/cron.d/mycron

cronの設定

コピーしたファイルを以下のように書き換えます。今回はpyenvを使っているので、pyenvのパスを通す処理を実行の前に加えています。また、> [path/to/your_log_file] 2>&1という記述によって、標準出力とエラーのログを指定したファイルに出力しています。cronの時間の設定はcronの設定方法等を参考にしてみて下さい。以下の例は毎日1時に定期実行です。

/etc/cron.d/mycron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user^ command
0 1    * * *  pi   PYENV_ROOT=~/.pyenv;export PATH=$PATH:$PYENV_ROOT/bin;eval "$(pyenv init -)";eval "$(pyenv init -)";eval "$(pyenv virtualenv-init -)";python [path/to/your_execution_file] > [path/to/your_log_file] 2>&1

cronの設定の反映

ファイルの設定ができたら、以下のコマンドでcronの変更を反映させます。

$ sudo service cron restart

以上で、定期実行の設定も完了です。

参考

8
13
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
8
13