Tenderlove Making

Mechanize One Liners

I thought I’d try to come up with some useful one liners for Mechanize. Here goes:

Fetch a page and print to stdout: ~~~ ruby puts WWW::Mechanize.new.get(ARGV[0]).body ~~~

List all links in a page: ~~~ ruby WWW::Mechanize.new.get(ARGV[0]).links.each { |l| puts l.text } ~~~

Visit all links on a page: ~~~ ruby (a = WWW::Mechanize.new).get(ARGV[0]).links.each { |l| puts a.click(l).body } ~~~

List all links that match a pattern: ~~~ ruby WWW::Mechanize.new.get(ARGV[0]).links.text(/[a-z]/).each { |l| puts l.text } ~~~

Visit all links that match a pattern: ~~~ ruby (a = WWW::Mechanize.new).get(ARGV[0]).links.text(/[a]/).each { |l| puts a.click(l).body } ~~~

Smaller Spider: ~~~ ruby (mech = WWW::Mechanize.new).get(ARGV[0]) (a = lambda { |p| mech.page.links.each { |l| mech.click(l) && p.call(p) if ! mech.visited? l } }).call(a) ~~~

« go back