What I need
Execute Nokogiri query, add a line break so when data is outputted its not bunched up.
data = doc.css('div#specifications div#spec-area ul.product-spec')[0].text
I tried this
data = doc.css('div#specifications div#spec-area ul.product-spec')[0].css('li').each{ |li| li.replace '\n' }.text
My Full Code
require 'Nokogiri'
require 'open-uri'
require 'spreadsheet'
doc = Nokogiri::HTML(open("http://www.asus.com/Notebooks_Ultrabooks/ASUS_TAICHI_21/#specifications"))
#Grab our product specifications - we only need the text not HTML
data = doc.css('div#specifications div#spec-area ul.product-spec')[0].text
#Create the Spreadsheet
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
sheet1.name = 'My First Worksheet'
#Output our data to the Spreadsheet
sheet1[0,0] = data
book.write 'C:/Users/Barry/Desktop/output.xls'
You don’t want to replace the list elements, you just want to map them to their texts and then join them together separated by newlines.
If you grab the elements with this:
Then you can get a list of all the list elements separated by linebreaks using
mapandjoin, like this:(From the comments) I’ve never used ruby spreadsheet before, but this should allow you to input the data:
Hope that helps.
p.s. “nokogiri” in
require "nokogiri"should be all lowercase.