const request = require("./src/http/http"); const config = require("./src/config/config"); const fs = require('fs'); let result = [] // 延迟函数,单位为毫秒 const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); const main = async () => { for (let index = 1; index <= 50; index++) { try { let { feeds } = await request.post(config.visionNewRecoFeed, { dailyFirstPage: false }); console.log(`第: ${index} 次爬`); console.log(`爬到的数据长度: ${feeds.length}`); console.log(``); feeds.forEach(v => { result.push({ video_introduce: v.photo.caption, video_url: v.photo.photoUrl, video_img: v.photo.coverUrl }); }); // 设置延迟时间(例如3000毫秒=3秒) await delay(1000); // 你可以调整这个值 } catch (error) { console.error(`第 ${index} 次请求出错:`, error); // 出错时也等待一段时间再继续 await delay(3000); } } // 先读取现有数据,然后追加新数据 fs.readFile('data.json', 'utf8', (err, data) => { if (err) { console.error('读取文件出错:', err); return; } const existingData = JSON.parse(data); console.log(`data.json 原文件数据长度: ${existingData.length}`); console.log(`爬到的新数据长度: ${result.length}`); const updatedData = [...existingData, ...result]; console.log(`合并后总数据长度: ${updatedData.length}`); fs.writeFile('data.json', JSON.stringify(updatedData, null, 2), (err) => { if (err) console.error('写入文件出错:', err); else console.log('数据已成功追加到data.json'); }); }); } main();