Excellent Scraping/Form Posting/Mining Mechanize Examples/Scripts (that Work!!!))) hehe
WWW::Mechanize examples
Google
require ‘rubygems’
require ‘mechanize’
agent = WWW::Mechanize.new
agent.user_agent_alias = ‘Mac Safari’
page = agent.get(“http://www.google.com/”)
search_form = page.forms.with.name(“f”).first
search_form.q = “Hello”
search_results = agent.submit(search_form)
puts search_results.body
Rubyforge
require ‘mechanize’
agent = WWW::Mechanize.new
page = agent.get(‘http://rubyforge.org/’)
link = page.links.text(/Log In/)
page = agent.click(link)
form = page.forms[1]
form.form_loginname = ARGV[0]
form.form_pw = ARGV[1]
page = agent.submit(form, form.buttons.first)
puts page.body
File Upload
This example uploads one image as two different images to flickr.
require ‘rubygems’
require ‘mechanize’
agent = WWW::Mechanize.new
# Get the flickr sign in page
page = agent.get(‘http://flickr.com/signin/flickr/’)
# Fill out the login form
form = page.forms.name(‘flickrloginform’).first
form.email = ARGV[0]
form.password = ARGV[1]
page = agent.submit(form)
# Go to the upload page
page = agent.click page.links.text(‘Upload’)
# Fill out the form
form = page.forms.action(‘/photos_upload_process.gne’).first
form.file_uploads.name(‘file1’).first.file_name = ARGV[2]
agent.submit(form)
Pluggable Parsers
Lets say you want html pages to automatically be parsed with Rubyful Soup. This example shows you how:
require ‘rubygems’
require ‘mechanize’
require ‘rubyful_soup’
class SoupParser e
$stderr.puts “#{e.class}: #{e.message}”
end
end
end
end
TestMech.new.process