You will eventually get 503 errors if you are running a scraper on a google search result page. A more scalable (and legal) approach is to use the Google's Custom Search API.
The API provides 100 search queries per day for free. If you need more, you may sign up for billing in the Google Developers Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day.
The example below get's Google search results in JSON format:
require 'open-uri'
require 'httparty'
require 'pp'
def get_google_search_results(search_phrase)
# assign api key
api_key = "Your api key here"
# encode search phrase
search_phrase_encoded = URI::encode(search_phrase)
# get api response
response = HTTParty.get("https://www.googleapis.com/customsearch/v1?q=#{search_phrase_encoded}&key=#{api_key}&num=100")
# pretty print api response
pp response
# get the url of the first search result
first_search_result_link = response["items"][0]["link"]
end
get_google_search_results("Top Movies in Theatres")