我有以下内容:
def self.parse_categories
categories = @data.css('#refinements ul').first
if categories.css('li:nth-child(1) a span').text == "Pet Supplies"
@categories_hash = {}
categories_category = categories.css('li:nth-child(2) strong').text
categories.css('li').drop(2).each do | categories |
categories_title = categories.css('.refinementLink').text
categories_count = categories.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@categories_hash[:categories] ||= {}
@categories_hash[:categories][categories_category] ||= {}
@categories_hash[:categories][categories_category][categories_title] = categories_count
end
else
@categories_hash = {}
end
return @categories_hash
end
现在代码在具有元素的页面中运行良好:#refinements ul
但是代码在没有元素的页面中中断。我该怎么做才能在没有时什么也不会发生#refinements ul
?就像忽略代码而不抛出错误或只是跳到else
?