LoginSignup
0
4

More than 3 years have passed since last update.

Selenium + WebDriver(Chrome) + Python | スクレイピングするための環境構築

Last updated at Posted at 2020-09-18

経緯

やりたかった事と、最初の状況

Webページ( js展開後のhtml ) をスクレイピングしたい。

※Python使ったことほぼない。
※Seleniumについては全く知らなかった状態。

調査開始

curlやphpでスクレイピングしようと思っていましたが、
curlではjs後のソースは拾ってこれないと分かり困りました。

そこで調べたところ、以下ふたつが候補にあがります。

phantomjs

たくさんの情報があり、それなりに有効な気がしましたが、2018年6月で開発終了、サポートも終了していたことがわかりました。

Selenium + WebDriver

調べると情報量も多く、新しめの記事も多いので、ひとまずSeleniumでやってみることにしました。

環境準備

必要なもの

python
pip
chromedriver
selenium

Macを使用していて標準でPythonが入っているため、Pythonのインストールについては割愛します。

pipをインストール

$ curl -kL https://bootstrap.pypa.io/get-pip.py | python

実行結果

$ curl -kL https://bootstrap.pypa.io/get-pip.py | python
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1841k  100 1841k    0     0  4630k      0 --:--:-- --:--:-- --:--:-- 4649k
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
  Downloading pip-20.2.3-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 4.0 MB/s 
Collecting wheel
  Downloading wheel-0.35.1-py2.py3-none-any.whl (33 kB)
Installing collected packages: pip, wheel
  WARNING: The scripts pip, pip2 and pip2.7 are installed in '/Users/xxx/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script wheel is installed in '/Users/xxx/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.2.3 wheel-0.35.1

パスを通せというメッセージがあるので、パスを通す

$ export PATH="$HOME/Library/Python/2.7/bin:$PATH"
$ echo 'export PATH="$HOME/Library/Python/2.7/bin:$PATH"' >> ~/.bash_profile

パスが通っているか確認

$ echo $PATH
/Users/xxx/Library/Python/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

$ cat ~/.bash_profile
export PATH="$HOME/Library/Python/2.7/bin:$PATH"

これでpipコマンドが使えるようになったので、確認

$ pip -V
pip 20.2.3 from /Users/xxx/Library/Python/2.7/lib/python/site-packages/pip (python 2.7)

Chromedriverをインストール

まず、現在パソコンで使用しているChromeのバージョンを確認する。
バージョン: 85.0.4181.101

ということで、以下のコマンドを使う

pip install chromedriver-binary==85.*

実行結果

$ pip install chromedriver-binary==85.*
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Collecting chromedriver-binary==85.*
  Downloading chromedriver-binary-85.0.4183.87.0.tar.gz (3.6 kB)
Building wheels for collected packages: chromedriver-binary
  Building wheel for chromedriver-binary (setup.py) ... done
  Created wheel for chromedriver-binary: filename=chromedriver_binary-85.0.4183.87.0-py2-none-any.whl size=7722067 sha256=901454e21156aef8f8bf4b0e302098747ea378a435c801330ea46d03ed
  Stored in directory: /Users/xxx/Library/Caches/pip/wheels/12/27/b7/69d38bfd65642b45a64e7e97e3160aba20f20be91cd5a
Successfully built chromedriver-binary
Installing collected packages: chromedriver-binary
Successfully installed chromedriver-binary-85.0.4183.87.0
$ 

Seleniumをインストール

使用コマンド

pip install selenium

実行結果

$ pip install selenium
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Collecting selenium
  Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
     |████████████████████████████████| 904 kB 5.2 MB/s 
Collecting urllib3
  Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 10.7 MB/s 
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.25.10
$ 

これで準備は完了です。

Pythonで動かしてみる

Pythonファイルを作成

test.py
import chromedriver_binary 
from selenium import webdriver

options = webdriver.ChromeOptions()
# options.add_argument('--incognito')
# options.add_argument('--headless')

print('connect...try...connect...try...')
driver = webdriver.Chrome(options=options)

driver.get('https://qiita.com')
print(driver.current_url)

# driver.quit()

実行

$ python test.py 

これでChromeのブラウザが立ち上がってくれます。 
めでたしめでたし。

シークレットウインドウで立ち上げるには、以下のコメントアウトを外してください。

options.add_argument('--incognito')

ヘッドレスブラウザで行う場合は、以下のコメントアウトを外してください。

options.add_argument('--headless')

あとは、Seleniumやxpathなどを調べていけば誰でもスクレイピングできると思います。

補足

今回Macに入っていたpythonのversionは2.7でしたので少し古く2020年1月でサポートが終了してます。普段Pythonを使わないのでそのままにしてますが、各コマンドの実行結果のところで、2.7に対するメッセージ(DEPRECATION)が出てしまっています。ご容赦くださいm(_ _)m

参考記事

pipインストール
https://qiita.com/suzuki_y/items/3261ffa9b67410803443
https://qiita.com/tom-u/items/134e2b8d4e11feea8e12

Seleniumセットアップ
https://qiita.com/Chanmoro/items/9a3c86bb465c1cce738a

Seleniumで要素を選択する方法まとめ
https://qiita.com/VA_nakatsu/items/0095755dc48ad7e86e2f

スクレイピング Xpath
https://qiita.com/rllllho/items/cb1187cec0fb17fc650a

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