Ruby: Mechanize get html body as string

Below ruby mechanize code will help you to retrieve the HTML content from the provide link.


agent = Mechanize.new { |a|
a.user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'
a.follow_meta_refresh = true
}

page = $agent.get("http://icfun.blogspot.com/")
content = page.body



Ruby mechanize is a very sophisticated library to perform lot of powerful task including mimicking the browser itself. You'll find lot of online tutorial regarding this issue.


Comments