麻烦与while循环和机械化Ruby

时间:2016-09-17 10:24:55

标签: ruby while-loop mechanize

我不明白为什么我的ruby代码不适用于while和mechanize。

require 'mechanize'
mechanize = Mechanize.new
j = 1
while j <= 30 do
    page = mechanize.get('http://mywebsite.com/events?page=#{j}'); j+=1
    i = 1
    while i <= 35 do
        link = page.link_with(:id => /detail-link-#{i}/); i += 1
        mylink = link.click
        url = mylink.uri
        title = mechanize.get(url)
        puts title.at('.container h1')
    end
end

我的while循环i工作但循环j没有进入下一页,我认为我的逻辑不好但我不知道问题出在哪里。

1 个答案:

答案 0 :(得分:2)

您尝试将页码插入到网页的网址中。但插值仅适用于双引号(")。

更改

page = mechanize.get('http://mywebsite.com/events?page=#{j}'); j+=1

page = mechanize.get("http://mywebsite.com/events?page=#{j}"); j+=1