Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Created March 12, 2023 15:34
Show Gist options
  • Save ezefranca/793ab0d1ecf6d0aa06187a0ea47f9fd0 to your computer and use it in GitHub Desktop.
Save ezefranca/793ab0d1ecf6d0aa06187a0ea47f9fd0 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
url = "https://www.auchan.pt/pt/pesquisa?q=#{query}&search-button=&lang=pt_PT&start=#{page * 24}&sz=24"
document = Nokogiri::HTML(open(url))
search_items = document.css('div.auc-product').map do |tile|
title = tile.css('a.link').text().capitalize
product, brand = title.split(' - ')
price = tile.css('span.value').text().strip
priceKg = tile.css('span.auc-measures--price-per-unit').text().strip.gsub('€/Kg', '')
unit = 'xx'
img_url = tile.css('img.tile-image').attr('data-src').value
brand = brand&.strip == 'Própria' ? 'Auchan' : brand&.strip
quantity = tile.css('span.auc-measures--avg-weight').text().strip
destiny_url = tile.css('a.auc-product-tile__image-container__image').attr('href').value
supermarket = 'Auchan'
{
title: title,
price: price,
price_kg: priceKg.empty? ? nil : priceKg,
unit: unit,
img_url: img_url,
brand: brand,
quantity: quantity,
destiny_url: "https://www.auchan.pt/pt#{destiny_url}",
supermarket: supermarket
}
end
{
query: query,
url: url,
page: page,
total_items: search_items.size,
search_items: search_items
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment