如何将这两个条件块代码集成到Ruby中?

时间:2011-06-24 00:20:11

标签: ruby nokogiri mechanize

如果我的代码在没有它们的情况下擦除,我如何整合这两个条件?我的代码已经运行了,但它会擦除所有行(非粗体和粗体值)并且不会刮掉title属性字符串。


条件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时

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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|
      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