Anaconda: ChromeDriverがインストールできない⇒別環境構築
Seleniumでのブラウザ自動化を行なおうとしたら、PhantomJSは廃止されてるっぽくて、Firefox用のgeckodriverは最新のFirefoxに対応できていないのか、ブラウザは起動するものの画面が表示されたまま先に進まない。
それで、chromedriverをインストールしようとしても、Python3.7には対応していないとエラーが出てしまう。
(base) > conda install python-chromedriver-binary Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: \ Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort. Examining certifi: 54%|█████████████████████████████████▉ | 21/39 [00:00<00:00, 71.11it/- Examining wheel: 92%|████████████████████████████████████████████████████████████ | 36/39 [00:00<00:00, 59.90it/s] Examining wincertstore: 97%|████████████████████████████████████████████████████████▌ | 38/39 [00:00<00:00, 59.90it/s]\ failed / UnsatisfiableError: The following specifications were found to be incompatible with the existing python installation in your environment: Specifications: - python-chromedriver-binary -> python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0'] Your python: python=3.7 If python is on the left-most side of the chain, that's the version you've asked for. When python appears to the right, that indicates that the thing on the left is somehow not available for the python version you are constrained to. Note that conda will not change your python version to a different minor version unless you explicitly specify that. The following specifications were found to be incompatible with each other: Package certifi conflicts for: python=3.7 -> pip -> setuptools -> certifi[version='>=2016.09|>=2016.9.26'] python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> pip -> setuptools -> certifi[version='>=2016.09|>=2016.9.26|>=2017.4.17'] Package ca-certificates conflicts for: python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> ca-certificates python=3.7 -> openssl[version='>=1.1.1a,<1.1.2a'] -> ca-certificates Package wincertstore conflicts for: python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> pip -> setuptools -> wincertstore[version='>=0.2'] python=3.7 -> pip -> setuptools -> wincertstore[version='>=0.2'] Package setuptools conflicts for: python=3.7 -> pip -> setuptools python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> pip -> setuptools Package pip conflicts for: python=3.7 -> pip python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> pip Package wheel conflicts for: python=3.7 -> pip -> wheel python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> pip -> wheel Package msgpack-python conflicts for: python-chromedriver-binary -> python[version='>=3.5,<3.6.0a0'] -> pip -> cachecontrol -> msgpack-python
このため、chromedriverメインで別環境を構築することにした。Pythonは3.6.9が選択された。
(base) >conda create -n selenium python-chromedriver-binary Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: C:\Users\oshiro\Anaconda3\envs\selenium added / updated specs: - python-chromedriver-binary The following packages will be downloaded: package | build ---------------------------|----------------- certifi-2019.11.28 | py36_0 157 KB chromedriver-binary-2.38 | 0 2.7 MB pip-19.3.1 | py36_0 1.9 MB python-3.6.9 | h5500b2f_0 15.9 MB python-chromedriver-binary-2.38.0| py36_0 48 KB setuptools-42.0.2 | py36_0 681 KB wheel-0.33.6 | py36_0 58 KB wincertstore-0.2 | py36h7fe50ca_0 14 KB ------------------------------------------------------------ Total: 21.5 MB The following NEW packages will be INSTALLED: certifi pkgs/main/win-64::certifi-2019.11.28-py36_0 chromedriver-bina~ pkgs/main/win-64::chromedriver-binary-2.38-0 pip pkgs/main/win-64::pip-19.3.1-py36_0 python pkgs/main/win-64::python-3.6.9-h5500b2f_0 python-chromedriv~ pkgs/main/win-64::python-chromedriver-binary-2.38.0-py36_0 setuptools pkgs/main/win-64::setuptools-42.0.2-py36_0 sqlite pkgs/main/win-64::sqlite-3.30.1-he774522_0 vc pkgs/main/win-64::vc-14.1-h0510ff6_4 vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_0 wheel pkgs/main/win-64::wheel-0.33.6-py36_0 wincertstore pkgs/main/win-64::wincertstore-0.2-py36h7fe50ca_0 Proceed ([y]/n)? y Downloading and Extracting Packages chromedriver-binary- | 2.7 MB | ############################################################################ | 100% python-3.6.9 | 15.9 MB | ############################################################################ | 100% setuptools-42.0.2 | 681 KB | ############################################################################ | 100% wincertstore-0.2 | 14 KB | ############################################################################ | 100% pip-19.3.1 | 1.9 MB | ############################################################################ | 100% certifi-2019.11.28 | 157 KB | ############################################################################ | 100% python-chromedriver- | 48 KB | ############################################################################ | 100% wheel-0.33.6 | 58 KB | ############################################################################ | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate selenium # # To deactivate an active environment, use # # $ conda deactivate (base) >conda activate selenium (selenium) >
これでChromeDriverのインストールはできた。
先の別環境の構築時に指定していてもよかったのだが、様子見で段階的に後からseleniumとBeautifulSoupのインストール。
まずは、Selenium:
(selenium) >conda install selenium Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: C:\Users\oshiro\Anaconda3\envs\selenium added / updated specs: - selenium The following packages will be downloaded: package | build ---------------------------|----------------- asn1crypto-1.2.0 | py36_0 162 KB cffi-1.13.2 | py36h7a1dbc1_0 227 KB cryptography-2.8 | py36h7a1dbc1_0 569 KB idna-2.8 | py36_0 111 KB pycparser-2.19 | py36_0 171 KB pyopenssl-19.1.0 | py36_0 87 KB pysocks-1.7.1 | py36_0 31 KB selenium-3.141.0 | py36he774522_0 805 KB six-1.13.0 | py36_0 27 KB urllib3-1.25.7 | py36_0 166 KB win_inet_pton-1.1.0 | py36_0 9 KB ------------------------------------------------------------ Total: 2.3 MB The following NEW packages will be INSTALLED: asn1crypto pkgs/main/win-64::asn1crypto-1.2.0-py36_0 ca-certificates pkgs/main/win-64::ca-certificates-2019.11.27-0 cffi pkgs/main/win-64::cffi-1.13.2-py36h7a1dbc1_0 cryptography pkgs/main/win-64::cryptography-2.8-py36h7a1dbc1_0 idna pkgs/main/win-64::idna-2.8-py36_0 openssl pkgs/main/win-64::openssl-1.1.1d-he774522_3 pycparser pkgs/main/win-64::pycparser-2.19-py36_0 pyopenssl pkgs/main/win-64::pyopenssl-19.1.0-py36_0 pysocks pkgs/main/win-64::pysocks-1.7.1-py36_0 selenium pkgs/main/win-64::selenium-3.141.0-py36he774522_0 six pkgs/main/win-64::six-1.13.0-py36_0 urllib3 pkgs/main/win-64::urllib3-1.25.7-py36_0 win_inet_pton pkgs/main/win-64::win_inet_pton-1.1.0-py36_0 Proceed ([y]/n)? y Downloading and Extracting Packages asn1crypto-1.2.0 | 162 KB | ############################################################################ | 100% six-1.13.0 | 27 KB | ############################################################################ | 100% cffi-1.13.2 | 227 KB | ############################################################################ | 100% pysocks-1.7.1 | 31 KB | ############################################################################ | 100% cryptography-2.8 | 569 KB | ############################################################################ | 100% win_inet_pton-1.1.0 | 9 KB | ############################################################################ | 100% urllib3-1.25.7 | 166 KB | ############################################################################ | 100% idna-2.8 | 111 KB | ############################################################################ | 100% selenium-3.141.0 | 805 KB | ############################################################################ | 100% pycparser-2.19 | 171 KB | ############################################################################ | 100% pyopenssl-19.1.0 | 87 KB | ############################################################################ | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done
次にBeautifulSoup:
(selenium) >conda install beautifulsoup4 Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: C:\Users\oshiro\Anaconda3\envs\selenium added / updated specs: - beautifulsoup4 The following packages will be downloaded: package | build ---------------------------|----------------- beautifulsoup4-4.8.1 | py36_0 155 KB soupsieve-1.9.5 | py36_0 61 KB ------------------------------------------------------------ Total: 216 KB The following NEW packages will be INSTALLED: beautifulsoup4 pkgs/main/win-64::beautifulsoup4-4.8.1-py36_0 soupsieve pkgs/main/win-64::soupsieve-1.9.5-py36_0 Proceed ([y]/n)? y Downloading and Extracting Packages beautifulsoup4-4.8.1 | 155 KB | ############################################################################ | 100% soupsieve-1.9.5 | 61 KB | ############################################################################ | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done (selenium) >
これで使用の準備が整った。
下記を実行するとChromeが立ち上がって、Googleにアクセスしそのページタイトルを印字する。ヘッダレスでも起動できるようだが、開発段階ではブラウザが表示されていたほうが動作が確認しやすそう。
from selenium import webdriver browser = webdriver.Chrome() # ブラウザとしてchromedriverを選択 browser.get("http://www.google.com") # Googleにアクセス print(browser.title) # ページタイトル「Google」と表示 browser.close() # タブ?を閉じる browser.quit() # ブラウザ終了