The Mechanize library is used for automating interaction with a website. It can follow links, and submit forms. Form fields can be populated and submitted. A history of URL‘s is maintained and can be queried.
require 'rubygems' require 'mechanize' require 'logger' agent = Mechanize.new { |a| a.log = Logger.new("mech.log") } agent.user_agent_alias = 'Mac Safari' page = agent.get("http://www.google.com/") search_form = page.form_with(:name => "f") search_form.field_with(:name => "q").value = "Hello" search_results = agent.submit(search_form) puts search_results.body
VERSION | = | '1.0.0' | The version of Mechanize you are using. | |
AGENT_ALIASES | = | { 'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'Windows IE 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', 'Windows Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6', 'Mac Safari' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10', 'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6', 'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401', 'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624', 'Linux Firefox' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1', 'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)', 'iPhone' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3', 'Mechanize' => "WWW-Mechanize/#{VERSION} (http://rubyforge.org/projects/mechanize/)" | User Agent aliases |
redirect_ok | -> | follow_redirect? |
ca_file | [RW] | |
cert | [RW] | |
conditional_requests | [RW] | |
cookie_jar | [RW] | |
follow_meta_refresh | [RW] | |
gzip_enabled | [RW] | |
history | [R] | |
history_added | [RW] | |
html_parser | [RW] | The HTML parser to be used when parsing documents |
html_parser | [RW] | |
keep_alive | [RW] | |
keep_alive_time | [RW] | |
key | [RW] | |
log | [RW] | |
open_timeout | [RW] | |
pass | [RW] | |
pluggable_parser | [R] | |
proxy_addr | [R] | Proxy settings |
proxy_pass | [R] | |
proxy_port | [R] | |
proxy_user | [R] | |
read_timeout | [RW] | |
redirect_ok | [RW] | |
redirection_limit | [RW] | |
request_headers | [RW] | A hash of custom request headers |
scheme_handlers | [RW] | |
user_agent | [RW] | |
verify_callback | [RW] | |
watch_for_set | [RW] |
DELETE to url with query_params, and setting options:
delete('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})
Fetches the URL passed in and returns a page.
HEAD to url with query_params, and setting options:
head('http://tenderlovemaking.com/', {'q' => 'foo'}, :headers => {})
Posts to the given URL with the request entity. The request entity is specified by either a string, or a list of key-value pairs represented by a hash or an array of arrays.
Examples:
agent.post('http://example.com/', "foo" => "bar") agent.post('http://example.com/', [ ["foo", "bar"] ]) agent.post('http://example.com/', "<message>hello</message>", 'Content-Type' => 'application/xml')
PUT to url with entity, and setting options:
put('http://tenderlovemaking.com/', 'new content', :headers => {'Content-Type' => 'text/plain'})
Submit a form with an optional button. Without a button:
page = agent.get('http://example.com') agent.submit(page.forms.first)
With a button
agent.submit(page.forms.first, page.forms.first.buttons.first)
Runs given block, then resets the page history as it was before. self is given as a parameter to the block. Returns the value of the block.