News Feed Forums General Web Scraping What data can be scraped from Yelp.com using Ruby?

  • What data can be scraped from Yelp.com using Ruby?

    Posted by Salma Dominique on 12/20/2024 at 8:11 am

    Scraping Yelp.com using Ruby allows you to collect valuable business information such as names, ratings, locations, and reviews. Ruby’s open-uri for HTTP requests and nokogiri for parsing HTML provide a straightforward way to extract this data. By targeting Yelp’s search pages, you can gather data from multiple businesses in a specific category or location. Below is an example script for scraping business information from Yelp.

    require 'open-uri'
    require 'nokogiri'
    # Target URL
    url = 'https://www.yelp.com/search?find_desc=restaurants&find_loc=New+York'
    html = URI.open(url).read
    # Parse HTML
    doc = Nokogiri::HTML(html)
    # Extract business details
    doc.css('.container__09f24__21w3G').each do |business|
      name = business.css('.css-1egxyvc').text.strip rescue 'Name not available'
      rating = business.css('.i-stars__09f24__1T6rz').attr('aria-label')&.value rescue 'Rating not available'
      address = business.css('.css-e81eai').text.strip rescue 'Address not available'
      puts "Name: #{name}, Rating: #{rating}, Address: #{address}"
    end
    

    This script fetches a Yelp search results page, parses the HTML using Nokogiri, and extracts business names, ratings, and addresses. Handling pagination to navigate through multiple pages ensures a more complete dataset. Adding delays between requests helps avoid detection by Yelp’s anti-scraping mechanisms.

    Salma Dominique replied 2 days, 10 hours ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

Log in to reply.