我有一个脚本,用于扫描网上商店的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