From: "Allan A." Date: 2012-09-11T10:56:44+09:00 Subject: Re: Export to CSV has array items under a single row Hassan Schroeder wrote in post #1075385: > > What method are you calling and what arguments does it expect? > Does the object or objects you're supplying meet those expectations? > > I'm guessing not :-) I suppose you're correct, since I don't really know what I'm doing. =) To be honest, I've just tried following some sites & tutorials on site scraping as I'm just trying to grab some info from a vBulletin forum. I've been able to pull single items from multiple pages fine but this is pulling multiple items on the page. You can see the sample of what I'm pulling on the above link but here is my code: require 'nokogiri' require 'open-uri' require 'csv' @thread = Array.new @thumb = Array.new files = CSV.read("urls.csv") (0..files.length - 1).each do |index| puts files[index][0] #load HTML to Nokogiri doc = Nokogiri::HTML(open(files[index][0])) #find the hyperlink threadtd = doc.css('tbody#threadbits_forum_406 tr td.alt2 a').map { |link| link['href'] } #find the div with the background img thumbtd = doc.css('tbody#threadbits_forum_406 tr td.alt2 a div') @thread << threadtd @thumb << thumbtd end CSV.open("thumbs.csv", "wb:UTF-8") do |row| row << ["Thread", "Thumbnail"] (0..files.length - 1).each do |index| row << [ @thread[index], @thumb[index]] end end The output that I see is fine (though, I'd like to pull the 'background:url()' value rather than the whole empty div) but in the CSV, it's grouped in the one row and the div's aren't comma separated: Thread,Thumbnail "[""http://LINK1"", ""http://LINK2"", ""http://LINK3"", ""http://LINK4""]","
" Again, I'm sure I'm screwing up or leaving out something important and I apologize if it's something that should be obvious! -- Posted via http://www.ruby-forum.com/.