Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Created March 13, 2023 20:33
Show Gist options
  • Save ezefranca/e4e050a3e02cecf7144cb30e8459cf24 to your computer and use it in GitHub Desktop.
Save ezefranca/e4e050a3e02cecf7144cb30e8459cf24 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
Encoding.default_external = 'UTF-8'
query = params['query'] || 'banana'
page = (params['page'] || 0).to_i
MAX_ATTEMPTS = 10
attempts = 0
doc = nil
url = "https://www.elcorteingles.pt/supermercado/pesquisar/" + (page > 0 ? "#{page}" : "") + "?term=#{query}&search=text"
begin
doc = Nokogiri::HTML(open(url).read.strip)
rescue Exception => ex
puts "Error: #{ex}"
attempts += 1
retry if attempts < MAX_ATTEMPTS
end
if doc.nil?
puts "Scrape failed repeatedly"
# Do something about the persistent error
# so that you don't try to access a nil
# doc later on.
else
puts "Scrape successful"
# Do something with the scraped data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment