首先,我创建所需的关键字(线索)数组,然后执行 Xpath 查询,将结果输入 CSV。一切顺利,但电子表格需要更好的格式,以便最终用户可以复制和粘贴
有没有一种方法可以使用 CSV 或 Axslx 来实现我想要的外观
我的代码如下:
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
我的代码可以将整个数组添加到一行,但是我怎么说 foreach 数组中的项目将它添加到换行符?我试过 \n 但它没有用。
我得到的输出
我想要的输出