SHARE
TWEET

Untitled

Kuzminov Mar 9th, 2020 282 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'open-uri'
  2. require 'nokogiri'
  3. require 'curb'
  4. require 'csv'
  5. require 'pry'
  6.  
  7. url = 'https://www.petsonic.com/pienso-para-perros-acana-cachorros-razas-medianas.html'
  8. request = Curl.get(url)
  9. doc = Nokogiri::HTML(request.body_str)
  10.  
  11. product_list = doc.xpath('//*[@id="attributes"]/fieldset/div/ul/li/label')
  12. all_product_with_price = product_list.map.with_index do |value, index|
  13.   product_hash = {
  14.     name: value.xpath('//*[@id="center_column"]/div/div[2]/div[2]/div[2]/h1').text.strip,
  15.     size: value.xpath('//*[@id="attributes"]/fieldset/div/ul/li/label/span[1]')[index].text.strip,
  16.     price: value.xpath('//*[@id="attributes"]/fieldset/div/ul/li/label/span[2]')[index].text.strip,
  17.     image: value.xpath('//ul[@id="thumbs_list_frame"]/li/a')[index].to_s.scan(/[Hh]ttp.*thickbox.*?jpg/).join
  18.   }
  19.   product_hash[:image] = '' if product_hash[:image].nil?
  20.   product_hash
  21. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top