How to scrape websites with Ruby & Mechanize

Posted by : on

Category : Ruby

Hello There, Today I will discuss how to scrape a website using gem mechanize. Here is an example:

Introduction

Mechanize is a ruby gem to scrape the websites. It uses Nokogiri to parse the html data. You can find more details about it on Link.

Install gem

  • Run gem install mechanize or write gem 'mechanize' into your gemfile and run bundle install

Fetching data from the websites:

  • Create a ruby file and write the following set of code in that file:
 require 'mechanize'
 
 mechanize = Mechanize.new
 
 page = mechanize.get('URL_OF_THE_WEBSOTE_TOSCRAPEs')
 
 puts "TItle of the page is :#{ page.title}"
 # will print the title of the websites
 

Thanks!!



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in