使用机械化点击 google 个页面

Clicking through google pages with mechanize

我正在尝试弄清楚如何在 Ruby 的 mechanize gem 中使用 link_with 函数。我已经掌握了基本概念:

page = <site>
blah blah blah
next_page = page.link_with(:text => "Next")
page = link.click

然而,当我使用它进行一些测试时,它似乎运行得很慢,我要做的是使用 loop do 循环浏览 google 的前十页有一点 time 变量从 10 开始倒数,当 time 变量达到 0 时,我希望程序跳出循环。看起来它在工作,但它只从 google 中拉出第一个 link 并且只是坐在那里。

来源:

require 'mechanize'
require 'uri'

SEARCH = "test"

@agent = Mechanize.new
page = @agent.get('http://www.google.com/')
google_form = page.form('f')
google_form.q = "#{SEARCH}"
url = @agent.submit(google_form, google_form.buttons.first)
  url.links.each do |link|
    if link.href.to_s =~ /url.q/
      str = link.href.to_s
      str_list = str.split(%r{=|&})
      urls = str_list[1]
      urls_to_log = URI.decode(urls)
      puts urls_to_log
      time = 10
      loop do
        next_page = page.link_with(:text => 'Next')
        page = link.click
        time -= 1
      end
      if time == 0
        break
      end
    end
  end

我找到了一点参考 here。然而,它并没有真正用我理解的术语来解释它。

我做错了什么,它只是放在第一个 link 上,却无处可去?

要跟随 Next 链接,您需要做的就是:

while page = page.link_with(:text => 'Next').click
  # do something with page
end