Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Last active March 12, 2023 15:21
Show Gist options
  • Save ezefranca/a095f090fcf3874b90659ed86d537a31 to your computer and use it in GitHub Desktop.
Save ezefranca/a095f090fcf3874b90659ed86d537a31 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://lojaonline.intermarche.pt/26-famoes/produit/recherche?mot=#{query}"
document = Nokogiri::HTML(open(url))
search_items = document.css('ul.vignettes_produit > li.vignette_produit_info').map do |tile|
title = tile.css('div.vignette_info > p').text().gsub(tile.css('p.js-marque').text(), '').strip()
price_str = tile.css('div.vignette_prix > p').text().strip()
price, priceKg = price_str.split('€').map { |s| s.strip unless s.nil? }
img_url = tile.css('div.vignette_img > img').attr('src')&.value || tile.css('div.vignette_img > img').attr('data-original')&.value
brand = tile.css('p.js-marque').text().strip unless tile.css('p.js-marque').nil?
next if img_url.nil? || brand.nil?
quantity = tile.css('div.vignette_produit_quantite').text().strip()
unit = quantity&.split("\n")&.first&.strip()
unit = nil if unit&.empty?
quantity = quantity&.gsub("\n","")&.gsub(" ","")
quantity = nil if quantity&.empty?
destiny_url = url
supermarket = 'Intermarché'
{
title: title,
price: price,
price_kg: priceKg,
img_url: img_url,
brand: brand,
quantity: quantity,
unit: unit,
destiny_url: destiny_url,
supermarket: supermarket
}
end.compact
{
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