Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Created March 10, 2023 23:07
Show Gist options
  • Save ezefranca/928bd414f50792fc5055f6c44de11d29 to your computer and use it in GitHub Desktop.
Save ezefranca/928bd414f50792fc5055f6c44de11d29 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'].to_i || 0
url = "https://www.celeiro.pt/catalogsearch/result/?q=#{query}"
document = Nokogiri::HTML(open(url))
search_items = document.css('ol.product-items > li.item').map do |tile|
title = tile.css('a.product-item-link').text()
price = tile.css('span.price').text()
img_url = tile.css('img.product-image-photo').attr('src')&.value
brand = tile.css('div.brand > a')&.text()
qty_info = tile.css('div.apresentacao').text().split('•').map(&:strip)
unit = qty_info.first if qty_info.size > 0
quantity = qty_info.last if qty_info.size > 1
destiny_url = tile.css('a.product-item-link').attr('href')&.value
supermarket = 'Celeiro'
{
title: title,
price: price,
unit: unit,
img_url: img_url,
brand: brand,
quantity: quantity,
destiny_url: destiny_url,
supermarket: supermarket
}
end
{
query: query,
page: page,
search_items: search_items,
url: url
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment