C
C
carpantas2017-01-12 14:15:26
ruby
carpantas, 2017-01-12 14:15:26

How to find lists of elements on the page after Nokogiri using XPath in Ruby?

This page is given:
Here is the parsing page
. On it, you need to find price and weight elements and save these pairs in an array. So far, I only get as many pairs of copies as there are total items on the page (i.e. if there are three weights in a product category, then I get three identical price tags and weights).

require 'open-uri'
require 'nokogiri'

url = 'http://www.petsonic.com/es/perros/snacks-y-huesos-perro/galletas-granja-para-perro'
html = open(url)

doc = Nokogiri::HTML(html)

names = []
doc.xpath('//*[@class = "attribute_list"]/*' ).each do |row|


  tempName = row.at_xpath('//span[@class = "attribute_name"]').text.strip
  tempPrice = row.at_xpath('//span[@class = "attribute_price"]').text.strip

  names.push(
    name: tempName,
    price: tempPrice
  )
end

puts names

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Bordenyuk, 2017-01-12
@carpantas

#...
  tempName = row.search('span.attribute_name').text.strip
  tempPrice = row.search('span.attribute_price').text.strip
#...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question