LoginSignup
0
0

More than 3 years have passed since last update.

40日目、1ヶ月ぶりの3日坊主でスクレイピング(チャレンジ中)

Last updated at Posted at 2019-06-08

JavascriptでどうにかなってるサイトをWebスクレイピングしたい!

というわけで、こちらの記事を参考にチャレンジしてみました。

Python Webスクレイピング テクニック集「取得できない値は無い」JavaScript対応@追記あり6/12

まずは、'pip install selenium' でインストール。

サンプルのindex.htmlをローカルに保存、日経平均が表示されるのを確認して
スクレイピングのコードを保存して実行!

$ python test.py
test.py:10: DeprecationWarning: use setter for headless property instead of set_headless
  options.set_headless(True)
test.py:13: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(chrome_options=options)
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/anaconda3/lib/python3.7/subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "/anaconda3/lib/python3.7/subprocess.py", line 1499, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    driver = webdriver.Chrome(chrome_options=options)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

10行目、headlessプロパティの代わりにsetterを使ってね。
13行目、chrome_optionsの代わりにoptionsを使ってね。

FileNotFoundErrorでchromedriverがないと。うーん?

そのほか13行目でもエラーが起きましたと?
chromedriverがPATHにないから入れたらって???
詳しくはこちらを見てねと。https://sites.google.com/a/chromium.org/chromedriver/home

うぐ。英語だ。がんばってGetting Startedを読んでみる。
ドライバーをダウンロードしてPATHを通しましょうと。よかったふつうだ。

2回目。/ApplicationsにコピーしてPATHを通す。

$ export PATH=$PATH:/Applications/chromedriver


$ python test.py
test.py:10: DeprecationWarning: use setter for headless property instead of set_headless
  options.set_headless(True)
test.py:13: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(chrome_options=options)
Traceback (most recent call last):
  File "test.py", line 13, in <module>
    driver = webdriver.Chrome(chrome_options=options)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/anaconda3/lib/python3.7/subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "/anaconda3/lib/python3.7/subprocess.py", line 1499, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
NotADirectoryError: [Errno 20] Not a directory: 'chromedriver'

うーん。
もう一度 Getting Startedを読んでみる。
・/usr/local/以下に置くといいらしい。
・Chromeのバージョンごとにドライバーがある!

3回目。ローカルのChromeのバージョンが74。ドライバーは76。合わせてみます。

NotADirectoryError: [Errno 20] Not a directory: 'chromedriver'

変わらず。
これではないらしい。

(所要時間 70分)

(追記)

brewでchromedriverがインストールできるって記事を見つけたのでやってみました。

brew install chromedriver
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (heroku/brew, homebrew/core and homebrew/cask).
==> New Formulae

(中略)

librealsense                             zstd
==> Renamed Formulae
gnatsd -> nats-server
==> Deleted Formulae
compose2kube        js-test-driver      safe                xmoto
erlang@18           minisat             tomcat@6
gtk-engines         node@6              typesafe-activator
gtk-murrine-engine  rlvm                whirr

Error: No available formula with the name "chromedriver" 
Found a cask named "chromedriver" instead.

???

パスが邪魔してるのかな?
chromedriverのパスを削除して再実行してみる

$ export PATH=/Users/........:/opt/X11/bin
$ brew install chromedriver
Error: No available formula with the name "chromedriver" 
Found a cask named "chromedriver" instead.

かわらず。
エラーメッセージでググってみたら、まったく同じケースの方がいました。

Selenium ChromeDriver & PythonをMacで動かせた。

brew cask install chromedriverしたらいいそうです。

$ brew cask install chromedriver
==> Satisfying dependencies
==> Downloading https://chromedriver.storage.googleapis.com/75.0.3770.8/chromedr
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'chromedriver'.
==> Installing Cask chromedriver
==> Linking Binary 'chromedriver' to '/usr/local/bin/chromedriver'.
🍺  chromedriver was successfully installed!

インストールできた!!!
そしてなぜビール!?
caskって何???


4度目の正直!

$ python test.py
test.py:10: DeprecationWarning: use setter for headless property instead of set_headless
  options.set_headless(True)
test.py:13: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(chrome_options=options)
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    driver.get("/Users/takamatsuyouko/Documents/Python/kujira/rensyu/index.html")
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
  (Session info: headless chrome=75.0.3770.80)

chromeが一瞬立ち上がって落ちる。
手元のchromeが74.xなのでバージョンが違うってこと!?
アップデートして、75.0.3770.80にしてみる。

5回目。同じエラーメッセージで落ちる。くそう。

6回目。再起動して再実行。

chromeが終了しなくてシャットダウンできない!

$ ps -ef | grep chrome
  501 40820     1   0  3:06PM ttys000    0:00.07 chromedriver --port=56238
  501 40932     1   0  3:11PM ttys000    0:00.05 chromedriver --port=56399
  501 40955 11785   0  3:16PM ttys000    0:00.00 grep chrome
$ kill -9 40820
$ kill -9 40932

再実行。同じくエラー。
うーん、なんなんだ。

(所要時間 60分)

0
0
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
0
0