在 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!
我不知道我是否正确理解你的问题,但如果这是动态循环大量页面并处理它们的问题,你可以这样做:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)