1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| const readline = require("readline"); const axios = require('axios'); const fs = require("fs"); const colors = require('colors');
let RL = readline.createInterface({ input: process.stdin, output: process.stdout })
function question(question) { return new Promise((resolve, reject) => { RL.question(`${question}\t`, function (value) { return resolve(value); }) }) }
var total = 0; var pageNum = 1; var pageSize = 30; var api = ""; var headers = ""; var params = "";
(async function () { const questions = ["请输入接口:", "请输入请求头:", "请输入参数:"]; for (let i = 0; i < questions.length; i++) { const value = await question(questions[i]); if (i === 0) { api = value; } else if (i === 1) { headers = value; } else { params = value; } } RL.close(); })()
RL.on("close", function () { console.log(`<<<------------------------- 开始爬取 ------------------------->>>\n`.blue); crawling(); })
async function getData() { const response = await axios({ url: api, method: "post", headers: { "Content-Type": "application/json", ...JSON.parse(headers) }, data: JSON.stringify({ ...JSON.parse(params), "pageSize": pageSize, "pageNum": pageNum }) }) return response.data; }
async function crawling() { const data = await getData(); console.log(data); if (data.code !== 0) { console.log('================= 数据读取失败 ================='.red); process.exit(0); }
console.log('================= 数据读取成功 ================='.green); total = data.total; const page = Math.ceil(total / pageSize); console.log(`共${page}页`); saveFile(data.rows, `第1页`); loading(); }
async function loading() { const page = Math.ceil(total / pageSize); for (let i = 1; i < page; i++) { pageNum++; const data = await getData(); saveFile(data.rows, `第${i + 1}页`); }
console.log(`<<<------------------------- 爬取完毕,已下载数据 ------------------------->>>\n`.bgGreen); total = 0; pageNum = 1; process.exit(0); }
function saveFile(res, name) { console.log(`<<<------------------------- 开始写入 ------------------------->>>\n`.blue); console.log(name); const writerStream = fs.createWriteStream("data/" + name + ".json"); writerStream.write(JSON.stringify(res)); writerStream.end();
writerStream.on('finish', function () { console.log(`<<<------------------------- 写入完成 ------------------------->>>\n`.green); }); writerStream.on('error', function (err) { console.log(err); console.log(`<<<------------------------- 写入错误 ------------------------->>>\n`.red); process.exit(0); }); }
|