From: "Jesús Gabriel y Galán" Date: 2013-02-05T00:46:10+09:00 Subject: Re: Nokogiri - Data output not as expected On Mon, Feb 4, 2013 at 4:39 PM, Barry Kavanagh wrote: > Sorry for bothering the forum about this but it is extremely > important. > > My Nokogiri output is being clobbered when being written to > excel, the output should be going row to row but its getting > overwritten. > > Anybody know why? :( > > Attachments: > http://www.ruby-forum.com/attachment/8106/asus_nokogiri.rb > > > -- > Posted via http://www.ruby-forum.com/. > require 'nokogiri' require 'open-uri' require 'spreadsheet' #Create the Spreadsheet Spreadsheet.client_encoding = 'UTF-8' book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet sheet1.name = 'My First Worksheet' #Specify our URI %w[ http://www.asus.com/Notebooks_Ultrabooks/S56CA/#specifications http://www.asus.com/Notebooks_Ultrabooks/ASUS_TAICHI_21/#specifications ].each do |url| doc = Nokogiri::HTML(open(url)) #Grab our product specifications data = doc.css('div#specifications div#spec-area ul.product-spec li') #Modify our data lines = data.map(&:text) #Output our data to the Spreadsheet lines.each.with_index do |line, i| a = 0 a+=1 sheet1[i, a] = line end end book.write 'C:/Users/Barry/Desktop/output.xls' I don't know the spreadsheet API, but when you call this: sheet1[i,a] = line a is always 1, because the two previous lines: a= 0 a += 1 always set a to 1. Maybe this means the first worksheet or whatever and it's fine, but in that case you should just write a 1: sheet1[i, 1] = line or at least have a more descriptive variable (or constant). And, also, for every URL, the lines.each is executed again, with i starting at 0. So every url will start at 0 and overwrite the previous lines. Jesus.