Search code examples
ruby-on-railsrubymechanize-ruby

Automated website interaction - Mechanzie - Rails


I'm using the Mechanize gem to automate interaction with a website form.

The site i'm trying to interact with is http://www.tastekid.com/like/books

I'm trying to automatically submit a string to query in the form and return the suggested books in an array.

Following the guide, i've pretty printed the page layout to find the form name, but, I am just finding a form with no name, nill:

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.tastekid.com/like/books')
pp page

How do I enter a string, submit the form and return the results in the form of an array?


Solution

  • These answers feel a little cluttered to me, so let me try to make it simpler:

    page = agent.get 'http://www.tastekid.com/like/books'
    

    there's only one form, so:

    form = page.form
    form['q'] = 'twilight'
    

    submit the form

    page = form.submit
    

    print the text from the a's

    puts page.search('.books a').map &:text