修改以下方法,以便我只调用Nokogiri :: HTML一次?

时间:2013-08-21 04:15:09

标签: ruby nokogiri

以下代码会废弃网站的不同部分。基本上我对每个部分都有一些方法,然后是一个方法(parse_details),它将所有哈希合并为一个:

class Parser
  def parse_details(html)
    merged_hashes = {}
    array_of_hashes = [
      self.parse_department(html),
      self.parse_super_saver(html),
    ]
    array_of_hashes.inject(merged_hashes,:update)

    return merged_hashes
  end

  def parse_department(file)  
    html       = file
    data       = Nokogiri::HTML(open(html))
    department = data.css('#ref_2619534011')

    @department_hash = {}
    department.css('li').drop(1).each do | department |
      department_title = department.css('.refinementLink').text
      department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @department_hash[:department] ||= {}
      @department_hash[:department]["Pet Supplies"] ||= {}
      @department_hash[:department]["Pet Supplies"][department_title] = department_count
    end 

    return @department_hash
  end 

  def parse_super_saver(file)
    html        = file
    data        = Nokogiri::HTML(open(html))
    super_saver = data.css('#ref_2661623011')

    @super_saver_hash = {}
    super_saver.css('li').each do | super_saver |
      super_saver_title = super_saver.css('.refinementLink').text
      super_saver_count = super_saver.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @super_saver_hash[:super_saver] ||= {}
      @super_saver_hash[:super_saver][super_saver_title] = super_saver_count
    end 

    return @super_saver_hash
  end

正如您所看到的,我正在多次呼叫Nokogiri::HTML(open(html))

有人建议我做这样的事情:

  def self.parse(html)
    doc = Nokogiri::HTML html 
    self.parse_details(doc) unless doc.nil?
  end

所以我只打电话给Nokogiri::HTML一次。

但是我被卡住了,例如我不知道如果他们使用新的department = data.css('#ref_2619534011')方法,他们应该使用parse这样的部分怎么办?我还对如何处理htmlfile参数感到困惑。使用新的parse方法后,我应该将它们留在那里还是删除它们?

关于如何实现我想要的任何建议?

1 个答案:

答案 0 :(得分:1)

简单转换:

class Parser
  def initialize(url)
    @data = Nokogiri.HTML(open(url))
  end
  def parse_details()
    {}.tap do |merged_hashes|
      array_of_hashes = [
        parse_department(),
        parse_super_saver(),
      ]
      array_of_hashes.inject(merged_hashes,:update)
    end
  end

  def parse_department()  
    department = @data.css('#ref_2619534011')

    @department_hash = {}
    department.css('li').drop(1).each do | department |
      department_title = department.css('.refinementLink').text
      department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @department_hash[:department] ||= {}
      @department_hash[:department]["Pet Supplies"] ||= {}
      @department_hash[:department]["Pet Supplies"][department_title] = department_count
    end 
    @department_hash
  end 

  def parse_super_saver()
    super_saver = @data.css('#ref_2661623011')

    @super_saver_hash = {}
    super_saver.css('li').each do | super_saver |
      super_saver_title = super_saver.css('.refinementLink').text
      super_saver_count = super_saver.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
      @super_saver_hash[:super_saver] ||= {}
      @super_saver_hash[:super_saver][super_saver_title] = super_saver_count
    end 
    @super_saver_hash
  end
end

如果您实际上不需要@department_hash@super_saver_hash作为实例变量,则可以选择将它们转换为我在tap中使用的parse_details样式。

如果你根本不需要将它作为一个类,而只是一组方法,那么请考虑:

module Parser
  def self.parse_details(url)
    data = Nokogiri.HTML(open(url))
    {}.tap do |merged_hashes|
      array_of_hashes = [
        parse_department(data),
        parse_super_saver(data),
      ]
      array_of_hashes.inject(merged_hashes,:update)
    end
  end

  def self.parse_department(data)
    {}.tap do |department_hash|
      data.css('#ref_2619534011 li').drop(1).each do | department |
        department_title = department.css('.refinementLink').text
        department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
        department_hash[:department] ||= {}
        department_hash[:department]["Pet Supplies"] ||= {}
        department_hash[:department]["Pet Supplies"][department_title] = department_count
      end
    end
  end 

  def self.parse_super_saver(data)    
    {}.tap do |super_saver_hash|
      data.css('#ref_2661623011 li').each do |super_saver|
        super_saver_title = super_saver.css('.refinementLink').text
        super_saver_count = super_saver.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
        super_saver_hash[:super_saver] ||= {}
        super_saver_hash[:super_saver][super_saver_title] = super_saver_count
      end 
    end
  end