我需要的
执行 Nokogiri 查询,添加一个换行符,以便在输出数据时不会将其捆绑在一起。
data = doc.css('div#specifications div#spec-area ul.product-spec')[0].text
我试过这个
data = doc.css('div#specifications div#spec-area ul.product-spec')[0].css('li').each{ |li| li.replace '\n' }.text
我的完整代码
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'