May 2008 Archives

Loving the BAMness of Mechanize

It's time to get excited about a README file. I just discovered Ruby's Mechanize gem, and all I can say is "wow." It's so BAM. How did I go for so long without appreciating the majesty of this library? Mechanize is a Ruby library that pretends to be a browser, follows redirects, simulates storing cookies, fills out forms, clicks on buttons and links, uploads files, selects out text with CSS selectors .... the only thing it can't do is wipe your butt for you. But if you could figure out how to make a browser do that, then you could automate it with Mechanize. http://mechanize.rubyforge.org/mechanize/files/GUIDE_txt.html http://mechanize.rubyforge.org/files/EXAMPLES_txt.html Check out this nice syntax: page = agent.get('http://google.com/') puts page.body BAM. You've just requested an HTML page. You want links? We got links: page.links.each do |link| puts link.text end BAM. That gives you a list of links on the page. You want to click on a link? page = agent.click page.links.text('News') BAM, you're on the next page. You want to fill out a form? google_form = page.form('f') # As in,
google_form.q = 'vitamin C' # Fill out the text box called "q" on the google search page. page = agent.submit(google_form, google_form.buttons.first) # Click on the submit button. BAM, you got search results about vitamin C! What? That search engine banned you for being a Ruby script? Pretend you're a Mac: agent.user_agent_alias = 'Mac Safari' BAM, you're back in business. What are the applications? Local integration testing exercising your full application stack, making sure your production machine is still up and running on every project you ever worked on, scraping, spamming, automated posting, spamming for fun and profit . . . . . you name it. Mechanize is a powerful tool that should be on the top shelf of every Ruby programmer's tool kit. I would say that with Mechanize, Capistrano, Flash widgets, RSpec, and Ruby on Rails you could pretty much rule the world. Continue Reading…

Posted by David Beckwith on May 08, 2008