Вопрос

I have this HTML:

<div class="pl-item-content clear" style="width: 176px; height: 385.875px;">
  <div class="pricing-info-container">
    <table cellspacing="0" class="product-prices">
      <colgroup>
        <col class="col-name"><col class="col-price">
      </colgroup>
      <tbody>
      <tr>
        <th class="col-name" scope="row">Prezzo a catalogo</th>
        <td class="col-price">96,09 €</td>
      </tr>
      <tr>
        <th class="col-name" scope="row">Prezzo</th>
        <td class="col-price">63,00 €</td>
      </tr>
      <tr>
        <th class="col-name" scope="row">Risparmio</th>
        <td class="col-price col-saving">34,4%</td>
      </tr>
      <tr>
        <th class="col-name" scope="row">Disponibilità</th>
        <td class="col-price"><div class="stock-value"><span>16</span></div></td>
      </tr>
      </tbody>
    </table>
  </div>
</div>

I have many pl-item-content blocks, so I need to iterate.

I need to find the prices and the % value: 96,09, 63,00, 34,4.

I'm using Nokogiri to parse the HTML document and extract some info. I have tried with this:

doc.css('div.pl-item-content').each do |item|
  puts item.at_css(".pricing-info-container .product-prices td.col-price").text.strip
end

The output is this:

96,09 €

The 63,03 € value is not present. I find only the first occurence and not all occurences. After this I need to find the % value but this is the second step.

Can you help me?


The solution is to use css instead at_css.

Это было полезно?

Решение

It works if you change it to

doc.css('div.pl-item-content').each do |item|
  puts item.css(".pricing-info-container .product-prices td.col-price").text.strip
end

On nokogiri documentation it says:

- (Object) at_css(*rules)
Search this node for the first occurrence of CSS rules. Equivalent to css(rules).first See Node#css for more information.

Другие советы

nokogiri's at_css does only return the first element that matches your query. Try something like that:

doc.search('div.pl-item-content').each do |table|
  table.search('table > tr').each do |row|
    puts row.at_css("td.col-price").text.strip
  end
end

Maybe still need some tweaks... go for it. If you du not care about which table actually delivered the data, just try this:

table.search('table > tr').each do |row|
  puts row.at_css("td.col-price").text.strip
end

Cheers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top