Ram Laxman Yadav bio photo

Ram Laxman Yadav

Ruby on Rails Developer

Email Twitter LinkedIn Github Stackoverflow

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!!