是否可以使用 Nokogiri 进行多域搜索。我知道您可以对单个域/页面进行多个 Xpath/CSS 搜索,但可以搜索多个域?
例如我想抓取http://www.asus.com/Notebooks_Ultrabooks/S56CA/#specifications和http://www.asus.com/Notebooks_Ultrabooks/ASUS_TAICHI_21/#specifications
我的代码
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
data = doc.css('div#specifications div#spec-area ul.product-spec li')
#Modify our data
lines = data.map(&: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
lines.each.with_index do |line, i|
sheet1[i, 0] = line
end
book.write 'C:/Users/Barry/Desktop/output.xls'