Created
May 1, 2013 20:17
-
-
Save jgrevich/5498030 to your computer and use it in GitHub Desktop.
example of google search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Scraper | |
class Google | |
include Capybara::DSL | |
Capybara.app_host = "http://www.google.com" | |
def search(query="I love Ruby!") | |
visit('/') | |
wait = 0 unless wait | |
sleep(wait) | |
wait += 5 | |
fill_in("q", :with => query) | |
click_button "Google Search" | |
result = [] | |
all("li.g h3").each do |h3| | |
a = h3.find("a") | |
result << "#{h3.text} => #{a[:href]}" | |
end | |
result | |
rescue Capybara::ElementNotFound | |
retry unless wait > 15 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment