-
Scrape special offers, user rating, product info from Marks & Spencer UK on Ruby
Scraping special offers, user ratings, and product ingredients from Marks & Spencer UK involves using Ruby with the Nokogiri gem for HTML parsing. The first step is to analyze the HTML structure of the webpage by inspecting it through browser developer tools. Special offers, which are typically highlighted prominently, can be located by identifying specific sections or tags that include terms like “Offer” or “Deal.” These elements are often listed alongside the product pricing.
User ratings are another crucial data point. They are typically represented as stars or numerical values displayed near the product reviews. Using Nokogiri, you can extract these ratings by locating the relevant HTML elements and cleaning the data for further use.
Ingredients for food or cosmetic products are usually listed in a detailed section. These details are often structured as a series of text entries, which can be extracted by targeting specific tags or classes in the HTML. Below is a complete Ruby script using Nokogiri to scrape special offers, user ratings, and product ingredients from Marks & Spencer UK:require 'nokogiri' require 'open-uri' # Fetch the webpage url = 'https://www.marksandspencer.com/product-page' doc = Nokogiri::HTML(URI.open(url)) # Scrape special offers offer = doc.at_css('.special-offer')&.text&.strip || 'No special offers available' puts "Special Offer: #{offer}" # Scrape user ratings rating = doc.at_css('.user-rating')&.text&.strip || 'No ratings available' puts "User Rating: #{rating}" # Scrape product ingredients ingredients = doc.css('.product-ingredients li').map(&:text).join(', ') puts "Ingredients: #{ingredients.empty? ? 'No ingredients listed' : ingredients}"
Log in to reply.