News Feed Forums General Web Scraping Scrape special offers, user rating, product info from Marks & Spencer UK on Ruby

  • Scrape special offers, user rating, product info from Marks & Spencer UK on Ruby

    Posted by Aiolos Fulvia on 12/14/2024 at 5:18 am

    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}"
    
    Ajeet Muhtar replied 4 days, 13 hours ago 5 Members · 4 Replies
  • 4 Replies
  • Scilla Phoebe

    Member
    12/14/2024 at 8:16 am

    The script could include better error handling to account for network errors or missing elements. For instance, wrapping scraping methods in begin-rescue blocks can prevent the script from crashing due to unexpected issues.

  • Shelah Dania

    Member
    12/17/2024 at 6:49 am

    Adding functionality to scrape multiple products by iterating over a list of URLs would improve the script’s versatility. This can be implemented using an array of URLs and a loop to process each one.

  • Hepsie Lilla

    Member
    12/17/2024 at 11:01 am

    To enhance the data output, you could save the scraped information in a structured format like JSON or CSV. This makes the data easier to analyze and share.

  • Ajeet Muhtar

    Member
    12/18/2024 at 6:35 am

    The script could also be improved by scheduling regular scraping sessions using tools like the Whenever gem. This would allow for continuous monitoring of product updates or changes in special offers.

Log in to reply.