Search code examples
rubycsvaxlsx

How do I add the contents of an array into a spreadsheet using Ruby


I am scraping a webpage at http://h10010.www1.hp.com/wwpc/ie/en/ho/WF06b/321957-321957-3329742-89318-89318-5186820-5231694.html?dnr=1

First I create my array of desired keywords (clues), I then perform an Xpath query feeding the results into CSV. Everything works smoothly but the spreadsheet needs better formatting so end users can copy and paste

Is there a way I can achieve my desired look using either CSV or Axslx

My code is below:

require 'rubygems'
require 'nokogiri'   
require 'open-uri'
require 'CSV'
require 'axlsx'

#Set encoding options to remove nasty Trademark symbols
  encoding_options = {
    :invalid           => :replace,  # Replace invalid byte sequences
    :undef             => :replace,  # Replace anything not defined in ASCII
    :replace           => '',        # Use a blank for those replacements
    :universal_newline => true       # Always break lines with \n
  }

doc = Nokogiri::HTML(open("http://h10010.www1.hp.com/wwpc/ie/en/ho/WF06b/321957-321957-3329742-89318-89318-5186820-5231694.html?dnr=1"))
#For each break create a ;
doc.css('br').each{ |br| br.replace ';' }

clues = Array.new
clues << 'Operating system'
clues << 'Processors'
clues << 'Chipset'
clues << 'Memory type'
clues << 'Hard drive'
clues << 'Graphics'
clues << 'Ports'
clues << 'Webcam'
clues << 'Pointing device'
clues << 'Keyboard'
clues << 'Network interface'
clues << 'Chipset'
clues << 'Wireless'
clues << 'Power supply type'
clues << 'Energy efficiency'
clues << 'Weight'
clues << 'Minimum dimensions (W x D x H)'
clues << 'Warranty'
clues << 'Software included'
clues << 'Product color'

CSV.open("output.csv", "wb") do |csv|
  #1. Output the Clues header
  #2. Scrape the output/force encoding to remove special characters
    csv << clues
    csv << clues.map{|clue| doc.at("//td[text()='#{clue}']/following-sibling::td").text.strip.encode Encoding.find('ASCII'), encoding_options}
  #end loop
end

My code can add the entire array to one line but how do I say foreach item in the array add it to a newline? I tried \n but it didn't work.

The Output I get

The output I get

My Desired output

My desired output


Solution

  • this is randym, the author of axlsx. I think you want to be doing this:

    clues = Array.new
    clues << 'Operating system'
    clues << 'Processors'
    clues << 'Chipset'
    clues << 'Memory type'
    
    Axlsx::Package.new do |p|
      p.workbook do |wb|
        wb.add_worksheet do |sheet|
          clues.each { |clue| sheet.add_row [clue] }
        end
      end
      p.serialize 'My_Spreadsheet.xlsx'
    end
    

    As to your second question:

    selector = "//td[text()='%s']/following-sibling::td"
    data = clues.map do |clue| 
             xpath = selector % clue
             [clue, doc.at(xpath).text.strip]
           end
    

    Then use

    data.each { |datum| sheet.add_row datum }
    

    when you build the worksheet

    require 'rubygems'
    require 'nokogiri'   
    require 'open-uri'
    require 'axlsx'
    
    doc = Nokogiri::HTML(open("http://h10010.www1.hp.com/wwpc/ie/en/ho/WF06b/321957-321957-3329742-89318-89318-5186820-5231694.html?dnr=1"))
    #For each break create a ;
    doc.css('br').each{ |br| br.replace ';' }
    
    clues = Array.new
    clues << 'Operating system'
    clues << 'Processors'
    clues << 'Chipset'
    clues << 'Memory type'
    clues << 'Hard drive'
    clues << 'Graphics'
    clues << 'Ports'
    clues << 'Webcam'
    clues << 'Pointing device'
    clues << 'Keyboard'
    clues << 'Network interface'
    clues << 'Chipset'
    clues << 'Wireless'
    clues << 'Power supply type'
    clues << 'Energy efficiency'
    clues << 'Weight'
    clues << 'Minimum dimensions (W x D x H)'
    clues << 'Warranty'
    clues << 'Software included'
    clues << 'Product color'
    
    selector = "//td[text()='%s']/following-sibling::td"
    data = clues.map do |clue| 
             xpath = selector % clue
             [clue, doc.at(xpath).text.strip]
           end
    
    Axlsx::Package.new do |p|
      p.workbook.add_worksheet do |sheet|
        data.each { |datum| sheet.add_row datum }
      end
      p.serialize 'output.xlsx'
    end
    

    Screen shots for your pleasure. enter image description here