使用Node.js将对象写入文件 [英] Write objects into file with Node.js
问题描述
我正在抓取给定URL页面的社交媒体链接,该函数返回一个包含URL列表的对象。
当我尝试将这些数据写入不同的文件时,它会以 [object Object]
的形式输出到文件中。而不是预期的:
[' https://twitter.com/#!/101Cookbooks ',
' http://www.facebook.com/101cookbooks ']
当我<$ c时
$ b
这是我在Node中读取和写入文件的悲哀尝试,试图读取(line,gotHTML):
fs.readFileSync('./ urls.txt')。toString()。split('\ n')。forEach(function(line){
console.log(line);
var obj = request(line,gotHTML);
console.log(obj);
fs.writeFileSync('./ data.json',obj,'utf-8');
});
供参考 - gotHTML
函数:
函数gotHTML(err,resp,html){
var social_ids = [];
if(err){
return console.log(err);
} else if(resp.statusCode === 200){
var parsedHTML = $ .load(html);
$ b parsedHTML('a')。map(function(i,link){
var href = $(link).attr('href'); $ b $如果(socialurls [i] .test(href)&& social_ids.indexOf(href)< 0){$ b $ for(var i = 0; i< socialurls.length; i ++) b social_ids.push(href);
};
};
})
};
return social_ids;
};
obj
是你的例子中的一个数组。
$ b
fs.writeFileSync(filename,data,[options])需要 String
或数据参数中的 Buffer
。 查看文档。
尝试将数组写入一个字符串格式:
//写入'https://twitter.com/#!/101Cookbooks','http:/ /www.facebook.com/101cookbooks'
fs.writeFileSync('./ data.json',obj.join(','),'utf-8');
或者:
//写入['https://twitter.com/#!/101Cookbooks','http://www.facebook.com/101cookbooks']
var util = require('util' );
fs.writeFileSync('./ data.json',util.inspect(obj),'utf-8');
编辑:您在示例中看到数组的原因是节点的 console.log
不会调用 toString
,它会调用 util.format
<
(请参阅console.js源文件)
I've searched all over stackoverflow / google for this, but can't seem to figure it out.
I'm scraping social media links of a given URL page, and the function returns an object with a list of URLs.
When I try to write this data into a different file, it outputs to the file as [object Object]
instead of the expected:
[ 'https://twitter.com/#!/101Cookbooks',
'http://www.facebook.com/101cookbooks']
as it does when I console.log()
the results.
This is my sad attempt to read and write a file in Node, trying to read each line(the url) and input through a function call request(line, gotHTML)
:
fs.readFileSync('./urls.txt').toString().split('\n').forEach(function (line){
console.log(line);
var obj = request(line, gotHTML);
console.log(obj);
fs.writeFileSync('./data.json', obj , 'utf-8');
});
for reference -- the gotHTML
function:
function gotHTML(err, resp, html){
var social_ids = [];
if(err){
return console.log(err);
} else if (resp.statusCode === 200) {
var parsedHTML = $.load(html);
parsedHTML('a').map(function(i, link){
var href = $(link).attr('href');
for(var i=0; i<socialurls.length; i++){
if(socialurls[i].test(href) && social_ids.indexOf(href) < 0 ) {
social_ids.push(href);
};
};
})
};
return social_ids;
};
obj
is an array in your example.
fs.writeFileSync(filename, data, [options]) requires either String
or Buffer
in the data parameter. see docs.
Try to write the array in a string format:
// writes 'https://twitter.com/#!/101Cookbooks', 'http://www.facebook.com/101cookbooks'
fs.writeFileSync('./data.json', obj.join(',') , 'utf-8');
Or:
// writes ['https://twitter.com/#!/101Cookbooks', 'http://www.facebook.com/101cookbooks']
var util = require('util');
fs.writeFileSync('./data.json', util.inspect(obj) , 'utf-8');
edit: The reason you see the array in your example is because node's implementation of console.log
doesn't just call toString
, it calls util.format
see console.js source
这篇关于使用Node.js将对象写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!