我如何重写这个填充@subcategories_urls_array
,就像“ Ruby 中的斐波那契序列(递归) ”。
我是否使用条件来检查是否没有更多'.group_title'
的 CSS 选择器来停止递归,或者是否可以使用计算周期的标志变量来完成?
def main
=begin
=end
@job_section_url = find_job_section_url()
write_header_to_file()
@groups_urls_array = describe_groups(@job_section_url)
@subcategories_urls_array = Array.new
@groups_urls_array.each do |group_url|
@subcategories_urls_array << describe_groups(group_url)
end #each
@subcategories_urls_array.flatten!
end #main
def describe_groups(job_section_url)
=begin
Parse a page into an array of groups URLs.
=end
# @looking_for_a_job_string = '%D0%98%D1%89%D1%83+%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D1%83'
@groups_urls_array = Array.new
@page = open(job_section_url, 'Cookie' => 'city=3')
@doc = Nokogiri::HTML(@page)
@nodeset = @doc.css('.group_title')[0..-2]
@nodeset.each do |a|
@group_url = CGI.escape(a['href']).gsub('%2F', '/')
@group_url = URI.join(DOMAIN_URL, @group_url).to_s
@groups_urls_array << @group_url
end #each
@groups_urls_array
end #describe_groups
我真的需要这个来实现吗?