5

Mechanize on Rubyでは、新しいページにアクセスするたびに新しい変数を割り当てる必要があります。例えば:

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

すべてのページ状態を保持する変数なしでMechanizeを実行する方法はありますか?お気に入り

  my_only_page.link_with(:text => "Continue").click!
  my_only_page.link_with(:text => "About").click!
4

1 に答える 1

10

あなたの質問を正しく理解しているかどうかはわかりませんが、多くのページを動的にループして処理する場合は、次のようにすることができます。

    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
于 2011-07-19T20:27:22.537 に答える