webスクレイピングをバックグラウンドで実行するには
webスクレイピングをしたまま、マルチユーザーでMacを使いたいのですがうまく行きません。
具体的にはpythonでPhantomJSを使用してwebスクレイピングしています。
コマンドから実行する分にはwebスクレイピングに成功しているのですが、ログアウトしたく、nohup, launch, screenなどを試していますがいずれもうまくいっていません。
詳細を以下に記載します。
手法1. nohup
以下が問題のPhantomJS を使用したpythonスクリプト(3.6.0)です。
普通に実行する分には成功します。
-------------------- test_nohup.py -----------------------------------
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
user_agent = 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36'
pjs_path = '/tmp/node_modules/phantomjs/bin/phantomjs'
dcap = {
"phantomjs.page.settings.userAgent" : user_agent,
}
driver = webdriver.PhantomJS(executable_path=pjs_path, desired_capabilities=dcap)
print("done.")
--------------------------------------------------------
nohupから呼ぶために、シェルスクリプト化しています。
------------------- test.sh -------------------------------------
#!/bin/sh
/usr/local/bin/python3 test_nohup.py
echo "end"
--------------------------------------------------------
#./test.sh
シェルから実行すればエラーなしで終了します。
これをnohupで呼ぶとエラーします。
#nohup ./test.sh &
[1]+ Stopped nohup ./test.sh
プロセスを殺して nohup.outを表示させると以下のエラーメッセージが出ています。
Traceback (most recent call last):
File "test_nohup.py", line 13, in <module>
driver = webdriver.PhantomJS(executable_path=pjs_path, desired_capabilities=dcap)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 52, in __init__
self.service.start()
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 96, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /tmp/node_modules/phantomjs/bin/phantomjs unexpectedly exited. Status code was: -1
手法2. launch
/Libarry/LauncdDaemons 上述の test.sh を実行するスクリプトを置いて実行してみましたが、エラーします。
エラーログは上とほとんど同じです。
Traceback (most recent call last):
File "test_nohup.py", line 13, in <module>
driver = webdriver.PhantomJS(executable_path=pjs_path, desired_capabilities=dcap)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 52, in __init__
self.service.start()
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 96, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /tmp/node_modules/phantomjs/bin/phantomjs unexpectedly exited. Status code was: 127
手法3. screen
screen を使用してみました。詳細は省略しますが、test_nohup.ph に web スクレイピング処理まで記載してみました。
screen を使って実行すると定期的にhtmlを取得してくれています。
シェルを落としても値は順調に取れているのですが、ログアウトして、再度ログインすると値の取得が途中で止まっていて再開されません。
予想としては、PhantomJSの処理がログアウト時に止まってしまっているのかと思っています。
おそらくPhantomJSがエラーしてしまうせいかと思っていますが、どう回避したら良いのかがわかりません。
やりたいこととしては、ログアウトして他のユーザーがログインできるようにしつつ、バックグラウンドでwebスクレイピングができれば手法は問いません。
PCは1台しかありません。
問題の解決法や他の手法など、ご存知の方がいればアドバイスお願いします。
iMac (21.5-inch Mid 2011), OS X El Capitan (10.11.5), null