Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在通过casperjs爬行后在json中插入数据?_Javascript_Ruby On Rails_Json_Casperjs - Fatal编程技术网

Javascript 如何在通过casperjs爬行后在json中插入数据?

Javascript 如何在通过casperjs爬行后在json中插入数据?,javascript,ruby-on-rails,json,casperjs,Javascript,Ruby On Rails,Json,Casperjs,我编写的代码解析了一些网页中的大量单词(innerHTML) require 'json' file = File.open(Rails.root.join('db','makeyourap.json')) contents = file.read json = ActiveSupport::JSON.decode(contents)["my_initial_words"] 我想直接将数据插入json文件 require 'json' file = File.open(Rails.root.j

我编写的代码解析了一些网页中的大量单词(innerHTML)

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
我想直接将数据插入json文件

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
这是我的js代码

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() {
        // echo results in some pretty fashion
        this.echo(words.length + ' links found:').exit();
        this.echo(words.join('\n')).exit();
});
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
结果是(例如)

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
因此,我想在我的json文件的“word”部分插入此数据(下面是json的示例代码)

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
并自动添加其他列(“键入”:“水果”和“拼写”:)

{ "my_initial_words": [
    {
    "type": "fruit",
    "word": "apple",
    "spell": "ap"
    },
    {
    "type": "fruit",
    "word": "banana",
    "spell": "ba"
    },
    {
    "type": "fruit",
    "word": "melon",
    "spell": "me"
    }   

]
}
----------------------------------------------------------------------------
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
感谢您添加更多答案!。。 但是我不知道我应该把这些代码放在哪里

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
你能不能再告诉我一次。。。您给我的代码执行“将结果保存到JSON文件?”因为我必须在我的
seeds.rb
文件中读取JSON文件(makeyourap.JSON),就像这样

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
通过casper写入文件 如果您想拥有一个文件,从中读取和写入内容,并附加内容,您可以这样做:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var fs = require('fs');
var FILENAME = 'makeyourap.json';
function add_new_fruits(fruits) {
    var data;
    if ( fs.isFile(FILENAME) ) {
        data = fs.read(FILENAME);
    } else {
        data = JSON.stringify({'my_initial_words' : [] });
    }
    var json = JSON.parse(data);
    fruits.forEach(function(word) {
        json.my_initial_words.push({"type": "fruit",
                                    "name": word,
                                    "spell": word.slice(0,2)});
    });
    data = JSON.stringify(json, null, '\t');
    fs.write(FILENAME, data, "w");
}
使用此选项,而不是旧的
this.echo
。就叫它

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js
这要么从文件中读取对象,要么在对象不存在时创建对象。然后,它附加来自新结果(包括重复项)的每个新对象,并将其写回
FILENAME

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
以前的方法:如何推出自己的 创建对象 因此,首先,您要创建一个对象,该对象只包含参数
my\u initial\u words
,其值如上所述

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
您可以通过以下方式创建函数:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function createFinal(wordArray) {
    var out = [];
    wordArray.forEach(function(word) {
        out.push({"type": "fruit", "name": word, "spell": word.slice(0,2)});
    });
    return out;
}
创建数组。然后,通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
到JSON Javascript有一个新的特性。使用javascript对象,如

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": ...
如上所述,使用

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
JSON.stringify(my_object) 
以获取要写入的表示

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
旧版本:通过重定向写入文件 以前,你有

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
this.echo(words.join('\n')).exit();
这给了你基本的清单。使用
this.echo
,尝试将其替换为

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
this.echo(JSON.stringify(my_object)).exit();
这将打印到标准输出。只需删除另一个
this.echo
行(找到150个单词),并通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js > makeyourap.json
如果要在casperjs中写入文件,请查看。

通过casper写入文件
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
如果您想拥有一个文件,从中读取和写入内容,并附加内容,您可以这样做:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var fs = require('fs');
var FILENAME = 'makeyourap.json';
function add_new_fruits(fruits) {
    var data;
    if ( fs.isFile(FILENAME) ) {
        data = fs.read(FILENAME);
    } else {
        data = JSON.stringify({'my_initial_words' : [] });
    }
    var json = JSON.parse(data);
    fruits.forEach(function(word) {
        json.my_initial_words.push({"type": "fruit",
                                    "name": word,
                                    "spell": word.slice(0,2)});
    });
    data = JSON.stringify(json, null, '\t');
    fs.write(FILENAME, data, "w");
}
使用此选项,而不是旧的
this.echo
。就叫它

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js
这要么从文件中读取对象,要么在对象不存在时创建对象。然后,它附加来自新结果(包括重复项)的每个新对象,并将其写回
FILENAME

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
以前的方法:如何推出自己的 创建对象 因此,首先,您要创建一个对象,该对象只包含参数
my\u initial\u words
,其值如上所述

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
您可以通过以下方式创建函数:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function createFinal(wordArray) {
    var out = [];
    wordArray.forEach(function(word) {
        out.push({"type": "fruit", "name": word, "spell": word.slice(0,2)});
    });
    return out;
}
创建数组。然后,通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
到JSON Javascript有一个新的特性。使用javascript对象,如

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": ...
如上所述,使用

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
JSON.stringify(my_object) 
以获取要写入的表示

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
旧版本:通过重定向写入文件 以前,你有

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
this.echo(words.join('\n')).exit();
这给了你基本的清单。使用
this.echo
,尝试将其替换为

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
this.echo(JSON.stringify(my_object)).exit();
这将打印到标准输出。只需删除另一个
this.echo
行(找到150个单词),并通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js > makeyourap.json
如果要在casperjs中写入文件,请查看。

通过casper写入文件
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
如果您想拥有一个文件,从中读取和写入内容,并附加内容,您可以这样做:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var fs = require('fs');
var FILENAME = 'makeyourap.json';
function add_new_fruits(fruits) {
    var data;
    if ( fs.isFile(FILENAME) ) {
        data = fs.read(FILENAME);
    } else {
        data = JSON.stringify({'my_initial_words' : [] });
    }
    var json = JSON.parse(data);
    fruits.forEach(function(word) {
        json.my_initial_words.push({"type": "fruit",
                                    "name": word,
                                    "spell": word.slice(0,2)});
    });
    data = JSON.stringify(json, null, '\t');
    fs.write(FILENAME, data, "w");
}
使用此选项,而不是旧的
this.echo
。就叫它

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js
这要么从文件中读取对象,要么在对象不存在时创建对象。然后,它附加来自新结果(包括重复项)的每个新对象,并将其写回
FILENAME

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
以前的方法:如何推出自己的 创建对象 因此,首先,您要创建一个对象,该对象只包含参数
my\u initial\u words
,其值如上所述

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
您可以通过以下方式创建函数:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function createFinal(wordArray) {
    var out = [];
    wordArray.forEach(function(word) {
        out.push({"type": "fruit", "name": word, "spell": word.slice(0,2)});
    });
    return out;
}
创建数组。然后,通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
到JSON Javascript有一个新的特性。使用javascript对象,如

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": ...
如上所述,使用

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
JSON.stringify(my_object) 
以获取要写入的表示

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
旧版本:通过重定向写入文件 以前,你有

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
this.echo(words.join('\n')).exit();
这给了你基本的清单。使用
this.echo
,尝试将其替换为

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
this.echo(JSON.stringify(my_object)).exit();
这将打印到标准输出。只需删除另一个
this.echo
行(找到150个单词),并通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js > makeyourap.json
如果要在casperjs中写入文件,请查看。

通过casper写入文件
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
如果您想拥有一个文件,从中读取和写入内容,并附加内容,您可以这样做:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var fs = require('fs');
var FILENAME = 'makeyourap.json';
function add_new_fruits(fruits) {
    var data;
    if ( fs.isFile(FILENAME) ) {
        data = fs.read(FILENAME);
    } else {
        data = JSON.stringify({'my_initial_words' : [] });
    }
    var json = JSON.parse(data);
    fruits.forEach(function(word) {
        json.my_initial_words.push({"type": "fruit",
                                    "name": word,
                                    "spell": word.slice(0,2)});
    });
    data = JSON.stringify(json, null, '\t');
    fs.write(FILENAME, data, "w");
}
使用此选项,而不是旧的
this.echo
。就叫它

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js
这要么从文件中读取对象,要么在对象不存在时创建对象。然后,它附加来自新结果(包括重复项)的每个新对象,并将其写回
FILENAME

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
以前的方法:如何推出自己的 创建对象 因此,首先,您要创建一个对象,该对象只包含参数
my\u initial\u words
,其值如上所述

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
您可以通过以下方式创建函数:

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function createFinal(wordArray) {
    var out = [];
    wordArray.forEach(function(word) {
        out.push({"type": "fruit", "name": word, "spell": word.slice(0,2)});
    });
    return out;
}
创建数组。然后,通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
到JSON Javascript有一个新的特性。使用javascript对象,如

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": ...
如上所述,使用

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
JSON.stringify(my_object) 
以获取要写入的表示

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
旧版本:通过重定向写入文件 以前,你有

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
this.echo(words.join('\n')).exit();
这给了你基本的清单。使用
this.echo
,尝试将其替换为

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
var my_object = { "my_initial_words": createFinal(words) };
this.echo(JSON.stringify(my_object)).exit();
这将打印到标准输出。只需删除另一个
this.echo
行(找到150个单词),并通过

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
casperjs application.js > makeyourap.json

如果您想在casperjs中写入文件,请查看。

那么,类似这样的内容

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function makeTypeObject(name, type) {
  return {
    name: name,
    type: type,
    spell: name.substr(0,2)
  };
}

var wordDesc = words.map(function (word) { 
   return makeTypeObject(word, "fruit"); 
});

var finalObject = {
  my_initial_words: wordDesc
};

var jsonString = JSON.stringify(finalObject);
// if you want prettyprint, try JSON.stringify(finalObject, null, "\t");

我希望这有帮助。

那么,像这样的事情

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function makeTypeObject(name, type) {
  return {
    name: name,
    type: type,
    spell: name.substr(0,2)
  };
}

var wordDesc = words.map(function (word) { 
   return makeTypeObject(word, "fruit"); 
});

var finalObject = {
  my_initial_words: wordDesc
};

var jsonString = JSON.stringify(finalObject);
// if you want prettyprint, try JSON.stringify(finalObject, null, "\t");

我希望这有帮助。

那么,像这样的事情

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function makeTypeObject(name, type) {
  return {
    name: name,
    type: type,
    spell: name.substr(0,2)
  };
}

var wordDesc = words.map(function (word) { 
   return makeTypeObject(word, "fruit"); 
});

var finalObject = {
  my_initial_words: wordDesc
};

var jsonString = JSON.stringify(finalObject);
// if you want prettyprint, try JSON.stringify(finalObject, null, "\t");

我希望这有帮助。

那么,像这样的事情

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]
function makeTypeObject(name, type) {
  return {
    name: name,
    type: type,
    spell: name.substr(0,2)
  };
}

var wordDesc = words.map(function (word) { 
   return makeTypeObject(word, "fruit"); 
});

var finalObject = {
  my_initial_words: wordDesc
};

var jsonString = JSON.stringify(finalObject);
// if you want prettyprint, try JSON.stringify(finalObject, null, "\t");

我希望这有帮助。

生成
类型
拼写
属性的规则是什么?实际上,我会像这样解析数据。。1.搜索“水果”和2。获取网页的innerhtml结果。所以在“类型”的情况下,我想我必须直接写“水果”,然后进行迭代和“拼写”
require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]