2011-06-24 24 views
0

如果我的代碼在沒有它們的情況下被刪除,我該如何整合這兩個條件?我的代碼已經在工作,但它會刪除所有行(非粗體和粗體值),並且不會刮擦title屬性字符串。如何在Ruby中集成這兩個條件塊代碼?


條件1:parses a table row only if one of its fields is bold

doc = Nokogiri::HTML(html) 
doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]').each do |row| 
puts row.at_xpath('td[3]/text()') 
end 

條件2:gets only the number off the title attribute string

doc  = Nokogiri::HTML(html) 
numbers = doc.xpath('//p[@title]').collect { |p| p[:title].gsub(/[^\d]/, '') } 

我的代碼:

doc = Nokogiri::HTML(search_result.body) 
rows = doc.css("table.articulos tr") 
i = 0 
details = rows.each do |row| 
    detail = {} 
    [ 
    [:sku, 'td[3]/text()'], 
    [:desc, 'td[4]/text()'], 
    [:qty, 'td[5]/text()'], 
    [:qty2, 'td[5]/p/b/text()'], 
    [:price, 'td[6]/text()'] 
    ].each do |name, xpath| 
    detail[name] = row.at_xpath(xpath).to_s.strip 
    end 
    i = i + 1 
    detail 
end 

第二次嘗試:

doc = Nokogiri::HTML(search_result.body) 
    rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]') 
    i = 0 
    details = rows.each do |row| 
    detail = {} 
    [ 
     [:sku, 'td[3]/text()'], 
     [:desc, 'td[4]/text()'], 
     [:stock, "td[5]/p[@title]"], 
     [:price, 'td[6]/text()'] 
    ].each do |name, xpath| 
     detail[name] = row.at_xpath(xpath).to_s.strip 

     end 
    i = i + 1 
    if detail[:sku] != "" 
      price = detail[:price].split 

      if price[1] == "D" 
       currency = 144 
      else 
       currency = 168 
      end 
      stock = detail[:stock].gsub(/[^\d]/, '-') 
      cost = price[0].gsub(",", "").to_f 
    end 

股票,而不是隻刮標題字符串它刮掉整款

<p-style="margin-top:-0px;-margin-bottom:0px;-cursor:hand"-title="2-en-su-sucursal"><b>10</b></p> 

時,我只想要從標題屬性中獲得2個

+0

我得到.collect的語法錯誤 – ingalcala

回答

0

這是我工作的代碼。也許需要一點清潔,但它的作品。結果是正確的,但我得到了很多nils。

doc = Nokogiri::HTML(search_result.body) 
rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]') 
i = 0 
details = rows.each do |row| 
    detail = {} 
    [ 
    [:sku, 'td[3]/text()'], 
    [:desc, 'td[4]/text()'], 
    [:stock, "td[5]/p/@title"], 
    [:price, 'td[6]/text()'] 
    ].each do |name, xpath| 
     detail[name] = row.at_xpath(xpath).to_s.strip 
    end 
    i = i + 1 
    if detail[:sku] != "" 
     price = detail[:price].split 

     if price[1] == "D" 
      currency = 144 
     else 
      currency = 168 
     end 
     stock = detail[:stock].each do |anchor| 
       puts anchor['title'] 
       end 
     stock1 = stock.gsub(/[^\d]/, '') 
     cost = price[0].gsub(",", "").to_f 
end 
0

這裏是我的未經測試的嘗試解決這個問題:

doc = Nokogiri::HTML(search_result.body) 
rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]') 
i = 0 
details = rows.each do |row| 
    detail = {} 
    [ 
    [:sku, 'td[3]/text()'], 
    [:desc, 'td[4]/text()'], 
    [:stock, 'td[5]/p[@title]'], 
    [:price, 'td[6]/text()'] 
    ].each do |name, xpath| 
    if name == :stock 
     detail[name] = row.at_xpath(xpath).collect { |p| p[:title].gsub(/[^\d]/, '') } 
    else 
     detail[name] = row.at_xpath(xpath).to_s.strip 
    end 
    end 
    i = i + 1 
    detail 
end