LoginSignup
4
2

More than 5 years have passed since last update.

RubyでSeleniumを用いたWEBスクレイピング(Windows)

Last updated at Posted at 2019-03-19

使用資材

  • rubyinstaller-2.6.1-1-x64.exe
  • IEDriverServer_Win32_3.14.0.zip
  • geckodriver-v0.24.0-win64.zip

Rubyのインストール

https://rubyinstaller.org/downloads/
上記URLの「WITHOUT DEVKIT」より、最新版をダウンロードし、全てデフォルトの設定のままでインストールを行う。(インストール完了後のMSYS2の起動は不要なので、チェックを外す)
インストール後、コマンドプロンプトにてrubyのバージョンが取得できることを確認

>ruby -v
ruby 2.6.1p33 (2019-01-30 revision 66950) [x64-mingw32]

ライブラリのインストール

コマンドプロンプトにて下記を実行

> gem install selenium-webdriver -r
> gem install csv -r
> gem install nokogiri -r

社内環境等でProxyの設定が必要な場合は、下記のようなオプションを末尾に追加する。
-p [Proxyサーバアドレス]:[ポート番号]

IEドライバサーバのインストール

FireFoxを使用する場合は、本項目は不要です。
社内ではブラウザがIEしか使用できなかったため、IEのドライバサーバー(32bit)を使用した。

https://www.seleniumhq.org/download/
上記URLの「The Internet Explorer Driver Server」より32bit版のIEドライバサーバを取得し、解凍したファイルを「C:\Ruby26-x64\bin」に配置する。
64bitでは、何かの絡みでSeleniumの動作速度が遅くなるためNG(思い出し次第追記予定)

geckodriverのインストール

https://github.com/mozilla/geckodriver/releases
上記URLより、「geckodriver-v0.24.0-win64.zip」をインストールし、解凍したファイルを「C:\Ruby26-x64\bin」に配置する。

サンプルソース

社内サイトへのアクセスに使用したため作成したソースは載せられず。
下記ソースはGoogle起動処理まで。

#!/usr/bin/ruby
# encoding: utf-8
require 'rubygems'
require 'nokogiri'
require 'selenium-webdriver'
require 'csv'
require "kconv"

# Driverの定義
driver = Selenium::WebDriver.for :firefox
wait = Selenium::WebDriver::Wait.new(:timeout => 10)

# Google起動
driver.get "https://www.google.co.jp/"
4
2
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
4
2