How to scrape websites with Ruby & Mechanize | Ram Laxman Yadav

How to scrape websites with Ruby & Mechanize

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


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 =
     page = mechanize.get('URL_OF_THE_WEBSOTE_TOSCRAPEs')
     puts "TItle of the page is :#{ page.title}"
     # will print the title of the websites


Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer