const fs = require("fs"); const requestUtils = require("../../utils/requestUtils"); // 爬取的内容:https://www.nowcoder.com/exam/interview const saveTempFile = true; main(); async function main() { // 请求参数id let questionInfo = { questionJobId: 156, questionClassifyId: null, questionId: null, } // 获取 Job 的 QuestionClassify let getQuestionClassifyResult = await getQuestionClassify(questionInfo.questionJobId); for (let a = 0; a < getQuestionClassifyResult.length; a++) { const QuestionClassify = getQuestionClassifyResult[a]; questionInfo.questionClassifyId = QuestionClassify.questionClassifyId; // console.log(questionInfo); // // 获取 QuestionClassify 的 Filter // let jobQuestionFilterResult = await jobQuestionFilter(questionInfo.questionJobId, questionInfo.questionClassifyId); // 获取 QuestionClassify 的 Question let jobQuestionListResult = await jobQuestionList(questionInfo.questionJobId, questionInfo.questionClassifyId); for (let b = 0; b < jobQuestionListResult.subjectList.length; b++) { const Question = jobQuestionListResult.subjectList[b]; questionInfo.questionId = Question.questionId; console.log(questionInfo); // 获取 Question 的 QuestionDetail let jobQuestionDetailResult = await jobQuestionDetail(questionInfo.questionJobId, questionInfo.questionClassifyId, questionInfo.questionId); } } } async function get(url, functionName) { let json = await requestUtils.getApiResult(url); // saveTempFile && fs.writeFileSync(`./temp/${functionName}-1.json`, json); try { let response = JSON.parse(json); // saveTempFile && fs.writeFileSync(`./temp/${functionName}.json`, JSON.stringify(response, null, 2)); // console.log(response); if (response.code === 0) { saveTempFile && fs.writeFileSync(`./temp/${functionName}-data.json`, JSON.stringify(response.data, null, 2)); return response.data; } else { console.error("请求失败", json); return null; } } catch (err) { console.error("请求解析失败", err); return null; } } // 获取分类下面的题目分类 比如 前端工程师 下面的【全部题目】 async function getQuestionClassify(questionJobId) { let url = `https://www.nowcoder.com/api/questiontraining/interview/getQuestionClassify?questionJobId=${questionJobId}&_=${Date.now()}` return await get(url, `1 getQuestionClassify-${questionJobId}`); } // 获取题目上面的筛选条件 async function jobQuestionFilter(questionJobId, questionClassifyId) { let url = `https://www.nowcoder.com/api/questiontraining/interview/jobQuestionFilter?questionJobId=${questionJobId}&questionClassifyId=${questionClassifyId}&_=${Date.now()}` return await get(url, `2 jobQuestionFilter-${questionJobId}-${questionClassifyId}`); } // 获取题目列表 async function jobQuestionList(questionJobId, questionClassifyId) { let url = `https://www.nowcoder.com/api/questiontraining/interview/jobQuestionList?questionJobId=${questionJobId}&questionClassifyId=${questionClassifyId}&page=1&size=50000&_=${Date.now()}` return await get(url, `3 jobQuestionList-${questionJobId}-${questionClassifyId}`); } // 获取题目详情 async function jobQuestionDetail(questionJobId, questionClassifyId, questionId) { let url = `https://www.nowcoder.com/api/questiontraining/interview/jobQuestionDetail?questionId=${questionId}&questionJobId=${questionJobId}&questionClassifyId=${questionClassifyId}&_=${Date.now()}` console.log(url); return await get(url, `4 jobQuestionDetail-${questionJobId}-${questionClassifyId}-${questionId}`); }