我如何在两个街区之间进行选择:
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并且不断出现意外的关键字错误。如果第一个语句= []?
,我该如何选择第二个语句答案 0 :(得分: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