From 2878dbf1110f1b289bc3d6fd497d562eaa260324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sun, 6 Nov 2022 17:34:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E7=89=9B=E5=AE=A2?= =?UTF-8?q?=E7=BD=91=20=E9=9D=A2=E8=AF=95=E9=A2=98=E5=BA=93=20=E7=88=AC?= =?UTF-8?q?=E5=8F=96=E8=84=9A=E6=9C=AC=EF=BC=88=E5=AD=98=E4=B8=BAJSON?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nowcoder/exam_interview/.gitignore | 1 + nowcoder/exam_interview/main.js | 82 +++++++++++++++++++++++++++ nowcoder/exam_interview/temp/.gitkeep | 0 3 files changed, 83 insertions(+) create mode 100644 nowcoder/exam_interview/.gitignore create mode 100644 nowcoder/exam_interview/main.js create mode 100644 nowcoder/exam_interview/temp/.gitkeep diff --git a/nowcoder/exam_interview/.gitignore b/nowcoder/exam_interview/.gitignore new file mode 100644 index 0000000..b68e748 --- /dev/null +++ b/nowcoder/exam_interview/.gitignore @@ -0,0 +1 @@ +temp/*.json \ No newline at end of file diff --git a/nowcoder/exam_interview/main.js b/nowcoder/exam_interview/main.js new file mode 100644 index 0000000..c18a1c6 --- /dev/null +++ b/nowcoder/exam_interview/main.js @@ -0,0 +1,82 @@ +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}`); +} \ No newline at end of file diff --git a/nowcoder/exam_interview/temp/.gitkeep b/nowcoder/exam_interview/temp/.gitkeep new file mode 100644 index 0000000..e69de29