Ruby Mechanize: Follow a Link
Asked Answered
A

1

5

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

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

Is there a way to run Mechanize without a variable holding every page state? like

  my_only_page.link_with(:text => "Continue").click!
  my_only_page.link_with(:text => "About").click!
Americium answered 14/7, 2011 at 6:37 Comment(0)
R
10

I don't know if I understand your question correctly, but if it's a matter of looping through a lot of pages dynamically and process them, you could do it like this:

    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
Reasoning answered 19/7, 2011 at 20:27 Comment(1)
Great answer this is what I was looking for as well, I had it but your code is a lot nicer then my method.Landbert

© 2022 - 2024 — McMap. All rights reserved.