I have written code that extracts a large number of words (innerHTML) from various webpages.
and I am interested in directly inserting this data into a json file..
Below is the JavaScript code snippet...
var words = [];
var casper = require('casper').create();
function getWords() {
var words = document.querySelectorAll('td.subject a');
return Array.prototype.map.call(words, function(e) {
return e.innerHTML;
});
}
casper.start('http://www.todayhumor.co.kr/board/list.php?table=bestofbest', function() {
words = this.evaluate(getWords);
});
for (var i=2; i <=5; i++) {
casper.thenOpen('http://www.todayhumor.co.kr/board/list.php?table=bestofbest&page='+i, function() {
words = words.concat(this.evaluate(getWords));
});
}
casper.run(function() {
// Display results
this.echo(words.length + ' links found:').exit();
this.echo(words.join('\n')).exit();
});
Additionally,
I execute this code through terminal using the following command!
username@wow:~/workspace/app/assets/javascripts $ casperjs application.js
The result could be something like (example)
150 words found:
apple
banana
melon
kiwi
citrus
watermelon
passionfruit
mango
orange
...
Therefore, I aim to insert this data into the "word" section of my json file (as shown in the sample json code below)
Moreover, other columns ("type": "fruit" and "spell":) should be automatically included
{ "my_initial_words": [
{
"type": "fruit",
"word": "apple",
"spell": "ap"
},
{
"type": "fruit",
"word": "banana",
"spell": "ba"
},
{
"type": "fruit",
"word": "melon",
"spell": "me"
}
]
}
----------------------------------------------------------------------------
Thank you for providing additional information! However, I need guidance on where to incorporate this code
Can you please clarify which part of the code you provided executes the process of "Saving the results to JSON file?" as I intend to read the json file (makeyourap.json) in my seeds.rb
file as follows
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]