diff --git a/20220224/.keep b/20220224/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/20220224/fs.js b/20220224/fs.js new file mode 100644 index 0000000000000000000000000000000000000000..81146a06a131f9bef1cef820b24bbe55a2e02f60 --- /dev/null +++ b/20220224/fs.js @@ -0,0 +1,40 @@ +function load(url) { + var http = require('https'); + var pm = new Promise(function(resolve, reject) { + http.get(url, function(res) { + var html = ''; + res.on('data', function(read) { + html += read.toString(); + }); + res.on('end', function() { + resolve(html); + }); + }).on('error', function(e) { + reject(e) + }); + }); + return pm; +} +//借取网上获取网页内容 抓取一言api返回的数据 获取随机句子 存入 +function api(num,format) { + for (let i = 0; i < num; i++) { + load('https://v1.hitokoto.cn/?c=f&encode=text').then(function(read) { + let sz = []; + sz[i] = read.trim(); + if (sz[i]=="") { + sz[i]="未能获取到数据,请尝试重新获取" + } + const fs = require('fs'); + fs.writeFile(`./每日一句${i+1}.${format}`, sz[i], err => { + if (err) throw err; + // console.log(sz[i]) + }); + }); + } +} +//设置创建几个文件,格式默认为txt 可更改 +api(5,'txt'); + + + +