0

我将如何在两个块之间进行选择:

page.parser.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "realviewLink", " " ))]').each do |product|

page.parser.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "gbb-productTitle", " " ))]').each do |product|

我尝试添加 begin/recuse 和 if/else 并不断收到意外的关键字错误。如果第一个语句 = [],我如何选择第二个语句?

4

1 回答 1

0

从您的代码中,更简单的方法是:

first_xpath_expr = '//*[contains(concat( " ", @class, " " ), concat( " ", "realviewLink", " " ))]'
second_xpath_expr = '//*[contains(concat( " ", @class, " " ), concat( " ", "gbb-productTitle", " " ))]'
xpath_result = page.parser.xpath(first_xpath_expr).map { |product| process_first_xpath_expr(product) }
if xpath_result.blank?
  xpath_result = page.parser.xpath(second_xpath_expr).map { |product| process_second_xpath_expr(product) }
end
于 2012-07-12T03:08:29.797 回答