我应该如何在ruby中使用递归方法
我使用Mechanize编写了一个简单的web涂鸦程序,现在我陷入了如何递归获取下一页的困境,下面是代码我应该如何在ruby中使用递归方法,ruby,Ruby,我使用Mechanize编写了一个简单的web涂鸦程序,现在我陷入了如何递归获取下一页的困境,下面是代码 def self.generate_page #generate a Mechainze page object,the first page agent = Mechanize.new url = "http://www.baidu.com/s?wd=intitle:#{URI.encode(WORD)}%20site:sina.com.cn&rn=50&
def self.generate_page #generate a Mechainze page object,the first page
agent = Mechanize.new
url = "http://www.baidu.com/s?wd=intitle:#{URI.encode(WORD)}%20site:sina.com.cn&rn=50&gpc=stf#{URI.encode(TIME)}"
page = agent.get(url)
page
end
def self.next_page(n_page) #get next page recursively by click next tag showed in each pages
puts n_page
# if I dont use puts , I get nothing , when using puts, I get
#<Mechanize::Page:0x007fd341c70fd0>
#<Mechanize::Page:0x007fd342f2ce08>
#<Mechanize::Page:0x007fd341d0cf70>
#<Mechanize::Page:0x007fd3424ff5c0>
#<Mechanize::Page:0x007fd341e1f660>
#<Mechanize::Page:0x007fd3425ec618>
#<Mechanize::Page:0x007fd3433f3e28>
#<Mechanize::Page:0x007fd3433a2410>
#<Mechanize::Page:0x007fd342446ca0>
#<Mechanize::Page:0x007fd343462490>
#<Mechanize::Page:0x007fd341c2fe18>
#<Mechanize::Page:0x007fd342d18040>
#<Mechanize::Page:0x007fd3432c76a8>
#which are the results I want
np = Mechanize.new.click(n_page.link_with(:text=>/next/)) unless n_page.link_with(:text=>/next/).nil?
result = next_page(np) unless np.nil?
result # here the value is empty, I dont know what is worng
end
def self.get_page # trying to pass the result of next_page() method
puts next_page(generate_page)
# it seems result is never passed here,
end
def self.generate#页面#生成一个Mechanize页面对象,第一个页面
agent=Mechanize.new
url=”http://www.baidu.com/s?wd=intitle:#{URI.encode(WORD)}%20site:sina.com.cn&rn=50&gpc=stf{URI.encode(TIME)}”
page=agent.get(url)
页
结束
定义self.next_page(n_page)#通过单击每个页面中显示的next标记递归获取下一页
放n_页
#如果我不使用看跌期权,我将一无所获,当我使用看跌期权时,我将一无所获
#
#
#
#
#
#
#
#
#
#
#
#
#
#我想要的结果是什么
np=Mechanize.new。单击(n_page.link_with(:text=>/next/)除非n_page.link_with(:text=>/next/)。nil?
结果=下一页(np),除非np.nil?
结果#这里的值是空的,我不知道是什么
结束
def self.get_page#尝试传递next_page()方法的结果
放置下一个页面(生成页面)
#结果似乎从未在这里传递过,
结束
我遵循这两个链接
及
但还是不知道怎么回事。。希望有人能帮我。。谢谢您的代码有一些问题:
Mechanize.new
next_page
方法返回包含链中每个链接页的数组,可以编写以下代码:
# you should store the mechanize agent as a global variable
Agent = Mechanize.new
# a helper method to DRY up the code
def click_to_next_page(page)
Agent.click(n_page.link_with(:text=>/next/))
end
# repeatedly visits next page until none exists
# returns all seen pages as an array
def get_all_next_pages(n_page)
results = []
np = click_to_next_page(n_page)
results.push(np)
until !np
np = click_to_next_page(np)
np && results.push(np)
end
results
end
# testing it out (i'm not actually running this)
base_url = "http://www.baidu.com/s?wd=intitle:#{URI.encode(WORD)}%20site:sina.com.cn&rn=50&gpc=stf#{URI.encode(TIME)}"
root_page = Agent.get(base_url)
next_pages = get_all_next_pages(root_page)
puts next_pages
您的代码存在一些问题:
Mechanize.new
next_page
方法返回包含链中每个链接页的数组,可以编写以下代码:
# you should store the mechanize agent as a global variable
Agent = Mechanize.new
# a helper method to DRY up the code
def click_to_next_page(page)
Agent.click(n_page.link_with(:text=>/next/))
end
# repeatedly visits next page until none exists
# returns all seen pages as an array
def get_all_next_pages(n_page)
results = []
np = click_to_next_page(n_page)
results.push(np)
until !np
np = click_to_next_page(np)
np && results.push(np)
end
results
end
# testing it out (i'm not actually running this)
base_url = "http://www.baidu.com/s?wd=intitle:#{URI.encode(WORD)}%20site:sina.com.cn&rn=50&gpc=stf#{URI.encode(TIME)}"
root_page = Agent.get(base_url)
next_pages = get_all_next_pages(root_page)
puts next_pages
谢谢起初,我打算迭代执行,但在以下几行失败:
results.push(np)until!np np=点击下一页(np)np&&results.push(np)
你的代码真的帮了我很多忙!谢谢起初,我打算迭代执行,但在以下几行失败:results.push(np)until!np np=点击下一页(np)np&&results.push(np)
你的代码真的帮了我很多忙!