21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CapybaraとSeleniumを使ってGoogle検索するサンプルコード

Last updated at Posted at 2015-02-08

概要

CapybaraからSeleniumを使う綺麗なコードが分からなかったので、
Rubyによるクローラ開発技法を参考に簡単なサンプルを残しておきます。

仕様

hit_numの引数に渡したワードで検索して検索結果の数を返します。
検索結果の件数がない場合は、allメソッドの返り値がnilなので、分岐しています。

実装

#! /usr/bin/env ruby

require "capybara"
require "selenium-webdriver"

Capybara.current_driver = :selenium

module Crowler
  class Google
    include Capybara::DSL

    def hit_num keyword

      visit URI.escape("https://www.google.co.jp/search?q=" + keyword)

      result_status = all("#resultStats")[0]
      unless result_status.nil?
        result_status.text.match(/(\d+,)*\d+/)[0].gsub(",","").to_i
      else
        0
      end
    end

  end
end

blowser = Crowler::Google.new
print blowser.hit_num "Capybara Selenium"

参考文献

Amazon.co.jp: Rubyによるクローラー開発技法 巡回・解析機能の実装と21の運用例: るびきち, 佐々木 拓郎: 本

21
19
3

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
21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?