SHARE
TWEET

Untitled

Kuzminov Mar 9th, 2020 (edited) 299 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.  
  14.   if doc.xpath('//ul[@id="thumbs_list_frame"]/li/a')[value].nil?
  15.     image = искать вторую картику
  16.   else
  17.     doc.xpath('//ul[@id="thumbs_list_frame"]/li/a')[i].to_s.scan(/[Hh]ttp.*thickbox.*?jpg/)
  18.   end
  19.  
  20.   product_hash = {
  21.     name: value.xpath('//*[@id="center_column"]/div/div[2]/div[2]/div[2]/h1').text.strip,
  22.     size: value.xpath('//*[@id="attributes"]/fieldset/div/ul/li/label/span[1]')[index].text.strip,
  23.     price: value.xpath('//*[@id="attributes"]/fieldset/div/ul/li/label/span[2]')[index].text.strip,
  24.     # image: value.xpath('//img[contains(@id, "bigpic")]').first.attributes['src'].value
  25.     image: doc.xpath('//ul[@id="thumbs_list_frame"]/li/a')[i].to_s.scan(/[Hh]ttp.*thickbox.*?jpg/)
  26.   }
  27.   product_hash[:image] = image if product_hash[:image].nil?
  28.   product_hash
  29. end
  30.  
  31. CSV.open('file_name.csv', 'w+') do |csv|
  32.   csv << %w[Product Size Price Picture]
  33.   all_product_with_price.each do |product|
  34.     csv << [
  35.       product[:name],
  36.       product[:size],
  37.       product[:price],
  38.       product[:image]
  39.     ]
  40.   end
  41. 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