我有一個腳本,用於掃描網上商店的HTML文章頁面。我正在測試一組22頁,其中5篇文章頁面有產品說明,其他頁面沒有。使用Nokogiri進行網頁抓取:: HTML和Ruby - 輸出爲CSV問題
這個代碼把正確的信息在屏幕上:
if doc.at_css('.product_description')
doc.css('div > .product_description > p').each do |description|
puts description
end
else
puts "no description"
end
但現在我卡在如何從那裏我將它們寫入正確得到這個輸出找到的產品描述爲一個數組CSV文件。
嘗試了幾個選項,但他們都沒有工作到目前爲止。 如果我將puts description
替換爲@description << description.content
,則文章的所有描述都會以CSV的上面一行結尾,儘管它們不屬於該行中的文章。
當我還將@description = "no description"
替換爲「no description」時,我的CSV中的前14行每個都會收到1個「no description」字母。看起來很有趣,但這不完全是我需要的。
如果需要更多代碼,只需留言!
這是CSV代碼,我在腳本中使用:
CSV.open("artinfo.csv", "wb") do |row|
row << ["category", "sub-category", "sub-sub-category", "price", "serial number", "title", "description"]
([email protected] - 1).each do |index|
row << [
@categories[index],
@subcategories[index],
@subsubcategories[index],
@prices[index],
@serial_numbers[index],
@title[index],
@description[index]]
end
end
你能告訴我們你的CSV代碼嗎?混亂的 – gmaliar
部分呼喚你的CSV對象'row' – pguardiario
不明白,其他的數據顯示在CSV文件中正確的...只是描述行爲怪異 – user2215918