The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
WWW::Mechanize alike in Ruby

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Michael Neumann

Posts: 66
Nickname: backflash
Registered: May, 2003

Michael Neumann is fallen in Love with Ruby
WWW::Mechanize alike in Ruby Posted: Jan 18, 2005 6:18 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Michael Neumann.
Original Post: WWW::Mechanize alike in Ruby
Feed Title: Mike's Weblog
Feed URL: http://www.ntecs.de/blog-old/index.rss?cat=ruby&count=7
Feed Description: Blogging about Ruby and other interesting stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Michael Neumann
Latest Posts From Mike's Weblog

Advertisement
I hacked up a little library which I use in some small projects to automate login into web-pages, extracting data from them and similar things. Web::Unit didn’t work for my purposes, and it was easier to reinvent the wheel than to hack Web::Unit (of course I first tried to hack it…).

It depends on narf-lib (>= 0.6.3), more exactly, it’s version of htmltools, and requires ruby-current (due to limitations in the net/http library of 1.8.2). If you want to try it out with Ruby 1.8.2, just get the net/*.rb files from ruby-current and use these instead.

Features

  • Cookies (very preliminary, no expire etc.)
  • automatic redirect
  • Forms, Links

Be aware that it’s just a few-hour hack, to get my project done.

It’s still very generic and misses lots of usability methods. For example, I use find all the time, or have to pass the link that should be clicked to method click.

Example

Here’s a simple example, that logs into your rubyforge.org acount (pass your username and password as command-line argument).

  require 'mechanize'

  agent = WWW::Mechanize.new {|a| a.log = Logger.new(STDERR) }
  page = agent.get('http://rubyforge.org/')
  link = page.links.find {|l| l.node.text =~ /Log In/ }
  page = agent.click(link)
  form = page.forms[1]
  form.fields.find {|f| f.name == 'form_loginname'}.value = ARGV[0]
  form.fields.find {|f| f.name == 'form_pw'}.value = ARGV[1]
  page = agent.submit(form, form.buttons.first)
  puts page.body

Get it!

www.ntecs.de/viewcvs/viewcvs/WWW/

Read: WWW::Mechanize alike in Ruby

Topic: HTML Filtering For RedCloth Previous Topic   Next Topic Topic: Great Soks!! AutomaticSummary Is Cool!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use