我正在开发一个从网站上抓取以下数据的网络抓取工具。
我使用以下代码将数据保存到三个单独的(单维)JSON 文件中:
require 'mechanize'
@raw_groups_array = []
@raw_categories_array = []
@search_attributes = []
@groups_clean = []
@categories_clean = []
@categories_combined = []
@categories_hash = {}
# Initialize Mechanize object
a = Mechanize.new
# Begin magic
a.get('http://www.marktplaats.nl/') do |page|
groups = page.search('//*[(@id = "navigation-categories")]//a')
groups.each do |group|
@raw_groups_array.push(group)
@groups_clean.push(group.text)
a.get(group[:href]) do |page_2|
categories = page_2.search('//*[(@id = "category-browser")]//a')
categories.each do |category|
@raw_categories_array.push(category)
@categories_clean.push(category.text)
@categories_combined.push("#{group.text} | #{category.text}")
a.get(category[:href]) do |page_3|
search_attributes = page_3.search('//*[contains(concat( " ", @class, " " ), concat( " ", "heading", " " ))]')
search_attributes.each do |attribute|
@search_attributes.push("#{group.text} | #{category.text} | #{attribute.text}") unless attribute.text == 'Outlet '
# Uncomment the line below if you want to see what's going on.
# (it has minimal effect on performance)
puts "#{group.text} | #{category.text} | #{attribute.text}" unless attribute.text == 'Outlet '
end
end
end
end
end
end
# Write json files
File.open('json/prestige/prestige_groups.json', 'w') do |f|
puts '# Writing groups'
f.write(@groups_clean.to_json)
puts '|-----------> Done.'
end
File.open('json/prestige/prestige_categories.json', 'w') do |f|
puts '# Writing categories'
f.write(@categories_clean.to_json)
puts '|-----------> Done.'
end
File.open('json/prestige/prestige_combined.json', 'w') do |f|
puts '# Writing combined'
f.write(@categories_combined.to_json)
puts '|-----------> Done.'
end
File.open('json/prestige/prestige_search_attributes.json', 'w') do |f|
puts '# Writing search attributes'
f.write(@search_attributes.to_json)
puts '|-----------> Done.'
end
puts '# Finished.'
该代码有效。但是我很难重构它以创建以下格式的 ruby 哈希:
{
"category"=>{
"name"=>"#{category}",
"group"=>"#{group}",
"search_attributes"=>{
"1"=>"#{search_attributes[0]}",
"2"=>"#{search_attributes[1]}",
"."=>"#{search_attributes[.]}",
"i"=>"#{search_attributes[i]}", # depending on search_attributes.length
}
}
}
我试过这样的事情:
...
search_attributes.each do |attribute|
@categories_hash.store([:category][:name], category.text)
@categories_hash.store([:category][:group], group.text)
@categories_hash.store([:category][:search_attributes][:1], attribute.text)
end
...
但不断收到语法错误。
任何帮助,将不胜感激。
更新
Max 建议我试试
Hash#[]
但这将返回一个具有单个类别(最后一个类别)的哈希。search_attributes.each_with_index do |attribute, index|
@categories_hash[:category][:name] = category.text
@categories_hash[:category][:group] = group.text
@categories_hash[:category][:search_attributes][:"#{index}"] = attribute.text unless attribute.text == "Outlet "
end
我已粘贴完整代码 here .
最佳答案
您使用 Hash#store
是否有特殊原因? ?这种方法没有简单的方法。
使用 Hash#[]
好多了,我想。
@categories_hash[:category] ||= {}
@categories_hash[:category][:search_attributes] ||= {}
@categories_hash[:category][:search_attributes][:1] = attribute.text
||=
在尝试在其中存储某些内容之前,请确保已初始化 subhash。
关于ruby - 如何将数据保存到多维 Ruby 哈希,然后将哈希转换为单个 JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24289042/