1 Star 3 Fork 2

JYeontu/一键导出掘金文章

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.js 2.90 KB
一键复制 编辑 原始数据 按行查看 历史
JYeontu 提交于 8个月前 . 调整文章列表导出逻辑
const axios = require("axios");
const fs = require("fs");
const cheerio = require("cheerio");
const htmlToMd = require("html-to-md");
const config = require("./config.json");
const queryList = async (params) => {
return new Promise((resolve) => {
axios
.post(
"https://api.juejin.cn/content_api/v1/article/query_list?aid=2608&uuid=7248590178503558696&spider=0",
params
)
.then((response) => {
resolve(response.data);
})
.catch((error) => {
console.error(error);
});
});
};
const getDetail = async (id) => {
const response = await axios.get(`https://juejin.cn/post/${id}`);
const html = response.data;
const $ = cheerio.load(html);
const article = $(
"#juejin > div.view-container > main > div > div.main-area.article-area > article"
).html();
let title = $(
"#juejin > div.view-container > main > div > div.main-area.article-area > article > h1"
)
.text()
.replace(/\n|\t| /g, "");
title = title.replace(/\|/g, "-").trim();
const content = $("#article-root").html();
fs.writeFileSync(`html/${title}.html`, article);
const markdownContent = htmlToMd(content);
fs.writeFileSync(`md/${title}.md`, markdownContent);
console.log(`已导出文章《${title}》`);
};
async function sleep(times = 1000) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, times);
});
}
const getListJson = async () => {
let list = [];
const params = {
user_id: config.user_id,
sort_type: 2,
cursor: "0",
};
let has_more = true;
while (has_more) {
const res = await queryList(params);
params.cursor = parseInt(params.cursor) + 10 + "";
has_more = res.has_more;
res.data && list.push(...res.data);
console.log(`正在导出文章列表数据${params.cursor}/${res.count}`);
if(list.length === res.count) break;
await sleep(2000);
}
fs.writeFileSync("list.json", JSON.stringify(list, null, 2));
console.log("文章列表数据已全部导出");
};
const getDetailBylist = async () => {
let index = config.lastIndex || 0;
try {
const list = require("./list.json");
const len = list.length;
for (index; index < len; index++) {
const i = index;
console.log(`正在导出《${list[i].article_info.title}${i + 1}/${len}……`);
await getDetail(list[i].article_id);
await sleep(5000);
}
console.log("已全部导出");
} catch (err) {
console.error(err);
config.lastIndex = index;
fs.writeFileSync("config.json", JSON.stringify(config, null, 2));
console.log("导出出错终止,30秒后自动重试");
await sleep(30000);
getDetailBylist();
}
};
const createDir = async()=>{
if(!fs.existsSync("html")){
fs.mkdirSync('html');
}
if(!fs.existsSync("md")){
fs.mkdirSync('md');
}
};
const run = async () => {
await getListJson();
await createDir();
await getDetailBylist();
};
run();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zheng_yongtao/one-click-export-juejin-article.git
git@gitee.com:zheng_yongtao/one-click-export-juejin-article.git
zheng_yongtao
one-click-export-juejin-article
一键导出掘金文章
master

搜索帮助