Reputation: 2299
I'm using the following code to generate a JSON file containing all category information for a particular website.
require 'mechanize'
@categories_hash = {}
@categories_hash['category'] ||= {}
@categories_hash['category']['id'] ||= {}
@categories_hash['category']['name'] ||= {}
@categories_hash['category']['group'] ||= {}
@categories_hash['category']['search_attributes'] ||= {}
# Initialize Mechanize object
a = Mechanize.new
# Open file and begin
File.open("json/booyah/#{Time.now.strftime '%Y%m%d%H%M%S'}_booyah_categories.json", 'w') do |f|
puts '# Writing category data to JSON file'
# Begin scraping
a.get('http://www.marktplaats.nl/') do |page|
groups = page.search('//*[(@id = "navigation-categories")]//a')
groups.each_with_index do |group, index_1|
a.get(group[:href]) do |page_2|
categories = page_2.search('//*[(@id = "category-browser")]//a')
categories.each_with_index do |category, index_2|
a.get(category[:href]) do |page_3|
search_attributes = page_3.search('//*[contains(concat( " ", @class, " " ), concat( " ", "heading", " " ))]')
search_attributes.each_with_index do |attribute, index_3|
@categories_hash['category']['id'] = "#{index_1}_#{index_2}"
@categories_hash['category']['name'] = category.text
@categories_hash['category']['group'] = group.text
@categories_hash['category']['search_attributes'][index_3] = attribute.text unless attribute.text == 'Outlet '
end
# Uncomment if you want to see what's being written
puts @categories_hash['category'].to_json
# Write the converted Hash to the JSON file
f.write(@categories_hash['category'].to_json)
end
end
end
end
end
puts '|-----------> Done.'
end
puts '# Finished.'
This code produces the following, invalid JSON file. Take a look at the full JSON file here. It looks like this:
{
"id": "0_0",
"name": "Boeken en Bijbels",
"group": "Antiek en Kunst",
"search_attributes": {
"0": "Prijs van/tot",
"1": "Groep en Rubriek",
"2": "Aangeboden sinds"
}
}{
"id": "0_1",
"name": "Emaille",
"group": "Antiek en Kunst",
"search_attributes": {
"0": "Prijs van/tot",
"1": "Groep en Rubriek",
"2": "Aangeboden sinds"
}
}{
"id": "0_2",
"name": "Gereedschap en Instrumenten",
"group": "Antiek en Kunst",
"search_attributes": {
"0": "Prijs van/tot",
"1": "Groep en Rubriek",
"2": "Aangeboden sinds"
}
}{...}
I want the output to be valid JSON and look like this:
[
{
"id": "0_0",
"name": "Boeken en Bijbels",
"group": "Antiek en Kunst",
"search_attributes": {
"0": "Prijs van/tot",
"1": "Groep en Rubriek",
"2": "Aangeboden sinds"
}
},
{
"id": "0_1",
"name": "Emaille",
"group": "Antiek en Kunst",
"search_attributes": {
"0": "Prijs van/tot",
"1": "Groep en Rubriek",
"2": "Aangeboden sinds"
}
},
{
"id": "0_2",
"name": "Gereedschap en Instrumenten",
"group": "Antiek en Kunst",
"search_attributes": {
"0": "Prijs van/tot",
"1": "Groep en Rubriek",
"2": "Aangeboden sinds"
}
},
{...}
]
The question is, how do I accomplish this?
A big thank you to maerics for his answer.
Here's the slightly updated, but working code:
require 'mechanize'
@categories_hash = {}
@categories_hash['category'] ||= {}
@categories_hash['category']['id'] ||= {}
@categories_hash['category']['name'] ||= {}
@categories_hash['category']['group'] ||= {}
@categories_hash['category']['search_attributes'] ||= {}
@hashes = []
# Initialize Mechanize object
a = Mechanize.new
# Begin scraping
a.get('http://www.marktplaats.nl/') do |page|
groups = page.search('//*[(@id = "navigation-categories")]//a')
groups.each_with_index do |group, index_1|
a.get(group[:href]) do |page_2|
categories = page_2.search('//*[(@id = "category-browser")]//a')
categories.each_with_index do |category, index_2|
a.get(category[:href]) do |page_3|
search_attributes = page_3.search('//*[contains(concat( " ", @class, " " ), concat( " ", "heading", " " ))]')
search_attributes.each_with_index do |attribute, index_3|
item = {
id: "#{index_1}_#{index_2}",
name: category.text,
group: group.text,
:search_attributes => {
:index_3.to_s => "#{attribute.text unless attribute.text == 'Outlet '}"
}
}
@hashes << item
puts item
end
end
end
end
end
end
# Open file and begin
File.open("json/light/#{Time.now.strftime '%Y%m%d%H%M%S'}_light_categories.json", 'w') do |f|
puts '# Writing category data to JSON file'
f.write(@hashes.to_json)
puts '|-----------> Done.'
end
puts '# Finished.'
Upvotes: 0
Views: 1232
Reputation: 156434
Using the builtin Ruby JSON library:
require 'json'
hashes = []
all_hashes.each { |h| hashes << h }
print hashes.to_json
Or, in the extreme case that your hashes will not fit into the available memory (pseudocode):
print '['
for each JSON hash H
print H
print ',' unless H is the last of the set
print ']'
Upvotes: 2