将网络数据写入TXT文件

冠胜

(我想使用node-fetch not request)

我想将网页(EX google.com)中的数据写入TXT文件(EX google.txt)中,但无法正常工作。当我运行脚本时,请检查我的“ google.txt”文件[object Object]

这是我的代码:

const fs = require('fs');


request('https://google.com', function (error, body) {

    fs.writeFile('google.txt', body, (err) => {
        if (err) throw err;

        console.log('Wrote google.com to google.txt !');
    });

});
布拉德

您所说的body实际上不是HTTP响应主体,而是响应对象。重命名它以使其清晰可见,然后使用该.body属性:

request('https://google.com', function (error, res) {

    fs.writeFile('google.txt', res.body, (err) => {
        if (err) throw err;

        console.log('Wrote google.com to google.txt !');
    });

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章