Ruby Mechanism: Follow the link

In Mechanize on Ruby, I have to assign a new variable for every new page that I come to. For instance:

  page2 = page1.link_with(:text => "Continue").click
  page3 = page2.link_with(:text => "About").click
  ...etc

Is there a way to start the Mechanism without a variable holding each state of the page? as

  my_only_page.link_with(:text => "Continue").click!
  my_only_page.link_with(:text => "About").click!
+5
source share
1 answer

I don’t know if I understood your question correctly, but if it is connected with the dynamic transition to many pages and their processing, you can do it as follows:

    require 'mechanize'

    url = "http://example.com"
    agent = Mechanize.new
    page = agent.get(url) #Get the starting page

    loop do
      # What you want to do on the page - ex. extract something...
      item = page.parser.css('.some_item').text
      item.save

      if link = page.link_with(:text => "Continue") # As long as there is still a nextpage link...
        page = link.click
      else # If no link left, then break out of loop
        break
      end
    end
+10
source

All Articles