以下代码删除了网站的不同部分。基本上我对每个部分都有一些方法,然后是一个方法 ( 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
方法?我也对如何处理html
andfile
参数感到困惑。有了新parse
方法后,我应该把它们留在那里还是移除它们?
关于如何完成我想要的任何建议?