Technology Feed Lab

technology site

Archive for the ‘Ruby’ Category

How to fetch webpage using Ruby and Mechanize

Posted by TechGeek on June 30, 2011

Few days back, we learned on how to fetch a webpage using Perl and Mechanize. This simple article explains how to fetch a webpage using Ruby and Mechanize. Ruby is yet another powerful programming language. It is an object-oriented programming language inspired by Perl. The syntax is very easy to understand and you can do lot more things with this language. If you want to learn Ruby you should download the Ruby installer here. Anyways, the code will be very similar as we seen in the earlier blog.

You will need to install the Mechanize first. You can do it by running Gem command like ‘gem install mechanize’. The Ruby uses ‘require’ to add and use that module. Here is very simple code to get you started,
——————————–-------------
#!/usr/bin/ruby

require 'mechanize'

agent = Mechanize.new
puts "Fetching webpage"

res = agent.get( 'http://www.google.com')

data = res.body

#Play with response data
puts data

—————————————————-

Once you will fetch the webpage, you can do anything you want using Ruby regular expressions. You can find all methods and examples on “http://mechanize.rubyforge.org/” website. This is very simple example for beginners. Hope this will be useful.

pixel How to fetch webpage using Ruby and Mechanize

Posted in How-To, Ruby, Technology | Tagged: , , | Leave a Comment »