LoginSignup
6
10

More than 3 years have passed since last update.

41日目、Chromeでスクレイピング、違う方法でやってみる。

Last updated at Posted at 2019-06-09

昨日の続きです。

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

昨日までのやり方だと動かないし、原因もわからないので
とにかく動くやり方を探してみることにしました。

こちらを参考にさせていただきました。

【Python】SeleniumでHeadless Chromeを使おう

まずはpipでchromedriverをインストールです。

$ pip install chromedriver-binary
Collecting chromedriver-binary
  Downloading https://files.pythonhosted.org/packages/97/79/dff51c0fd1eb6dea04762f6353581680b157e78de6ed866a08ee1c7773d0/chromedriver-binary-75.0.3770.8.0.tar.gz
Building wheels for collected packages: chromedriver-binary
  Running setup.py bdist_wheel for chromedriver-binary ... done
  Stored in directory: /Users/robamimim/Library/Caches/pip/wheels/30/27/2f/53691e12e1f4a1409f8893d76ea42ec737691a87ded39bbd66
Successfully built chromedriver-binary
Installing collected packages: chromedriver-binary
Successfully installed chromedriver-binary-75.0.3770.8.0
You are using pip version 18.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

いい感じです。
そして。

$ python test2.py
test2.py:9: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome(chrome_options=options)
(以下略)

chrome_optionsの代わりにoptionsを使えってこと?

before) driver = webdriver.Chrome(chrome_options=options)
after) driver = webdriver.Chrome(options=options)

これでいいのかな?

再実行。

$ python test2.py
Traceback (most recent call last):
  File "test2.py", line 9, in <module>
    driver = webdriver.Chrome(options=options)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  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.WebDriverException: Message: unknown error: no chrome binary at /Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary

9行目でこけた理由は、no chrome binary at /Applications/Google Chrome Canary.appだっていう。
うーん。Canaryってなんだろう。ひっかかります。調べてみました。

毎日更新されるデベロッパー向けビルド 最先端のウェブを体験(Chrome Canary は動作が不安定な場合があります) Chrome Canary をダウンロード

なるほど、最新版でしたか!
当然これはもっていないので、とりあえずコメントアウトでいいかな。
手元のChromeを見てくれたらいいんだけど。

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

# ブラウザーを起動
options = Options()
#options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

# Google検索画面にアクセス
driver.get('https://www.google.co.jp/')

# htmlを取得・表示
html = driver.page_source
print(html)

# ブラウザーを終了
driver.quit()
$ python test2.py
<html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head><meta charset="UTF-8"><meta content="世界中のあらゆる情報を検索するためのツールを提供しています。さまざまな検索機能を活用して、お探しの情報を見つけてください。" name="description"><meta content="noodp" name="robots"><meta content="/images/branding/googleg/1x/googleg_standard_
(以下略)

よさそうです!

そんでもってGoogle Colabで動いたらいいなーと思ったら。
ありました!!!

ColaboratoryでSeleniumが使えた:javascriptで生成されるページも簡単スクレイピング

助かりました。ありがとうございます。
(所要時間 90分)

6
10
4

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
10