{"id":19044618,"url":"https://github.com/bubao/zhihu-zhuanlan","last_synced_at":"2025-09-09T19:32:21.007Z","repository":{"id":207029422,"uuid":"89701227","full_name":"bubao/zhihu-zhuanlan","owner":"bubao","description":"知乎专栏爬虫","archived":false,"fork":false,"pushed_at":"2022-08-25T07:37:52.000Z","size":93,"stargazers_count":13,"open_issues_count":0,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T23:37:40.610Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bubao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-04-28T11:47:48.000Z","updated_at":"2023-08-16T03:27:29.000Z","dependencies_parsed_at":"2023-11-13T19:26:28.638Z","dependency_job_id":null,"html_url":"https://github.com/bubao/zhihu-zhuanlan","commit_stats":null,"previous_names":["bubao/zhihu-zhuanlan"],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/bubao/zhihu-zhuanlan","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fzhihu-zhuanlan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fzhihu-zhuanlan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fzhihu-zhuanlan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fzhihu-zhuanlan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bubao","download_url":"https://codeload.github.com/bubao/zhihu-zhuanlan/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fzhihu-zhuanlan/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274349197,"owners_count":25268974,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-09T02:00:10.223Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T22:46:53.864Z","updated_at":"2025-09-09T19:32:20.946Z","avatar_url":"https://github.com/bubao.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 知乎专栏爬虫 (zhihu-zhuanlan)\n\n\u003e by: bubao\n\u003e\n\u003e Created: 2017 年 04 月 28 日 20:00:44\n\u003e\n\u003e Modified : 2019-12-2 2:16:40\n\n知乎是一个好地方，最近有空，想把知乎上的文章爬下来，放在本地有空慢慢看。项目模仿 [zhangolve 的项目](https://github.com/zhangolve/zhihu-answer-convert-to-md-by-node)。\n\n## 怎么用\n\n### 获取\n\n```shell\ncnpm i https://github.com/bubao/GetZhiHuZhuanLan.git --save\n```\n\n### 使用\n\n`https://zhuanlan.zhihu.com/study-fe`的 `postID` 值是 `study-fe`：\n\n```js\nconst Zhuanlan = require(\"..\");\nconst fs = require(\"fs\");\nconst util = require(\"util\");\nconst FsStat = util.promisify(fs.stat);\nconst MAX_SAFE_INTEGER = 9007199254740991;\n\nfunction isLength(value) {\n    return (\n        typeof value === \"number\" \u0026\u0026\n        value \u003e -1 \u0026\u0026\n        value % 1 === 0 \u0026\u0026\n        value \u003c= MAX_SAFE_INTEGER\n    );\n}\n\nfunction isArrayLike(value) {\n    return (\n        value != null \u0026\u0026 typeof value !== \"function\" \u0026\u0026 isLength(value.length)\n    );\n}\n\nconst mkdir = async (...filePath) =\u003e {\n    if (isArrayLike(filePath)) {\n        filePath = require(\"path\").resolve(...filePath);\n    }\n    await FsStat(`${filePath}`).then(() =\u003e {\n        console.log(\n            `⚓  ${require(\"path\").basename(filePath)} 文件夹已经存在`\n        );\n    }).catch(() =\u003e {\n        fs.mkdir(`${filePath}`, () =\u003e {\n            console.log(\n                `🤖 创建 ${require(\"path\").basename(\n                    filePath\n                )}文件夹成功`\n            );\n        });\n    });\n};\n\nconst writeFile = (path, data, format) =\u003e {\n    fs.writeFile(`${path}.${format}`, data, \"utf8\", err =\u003e {\n        if (err) throw err;\n    });\n};\nconst run = (path, columnsID) =\u003e {\n    const zhihu = Zhuanlan.init({ columnsID });\n    let dir;\n    zhihu.once(\"info\", (data) =\u003e {\n        dir = data.title;\n        mkdir(`${path}/${data.title}`);\n    });\n    let write_count = 0;\n    zhihu.on(\"batch_data\", (element) =\u003e {\n        // console.log((element.now_count / element.articles_count * 100).toFixed(2) + \"%\");\n        element.data.map(({ filenameTime, header, content, copyRight, json }) =\u003e {\n            writeFile(\n                `${path}/${dir}/${filenameTime}`,\n                header + content + copyRight,\n                \"md\"\n            );\n            writeFile(\n                `${path}/${dir}/${filenameTime}`,\n                JSON.stringify(json),\n                \"json\"\n            );\n            write_count++;\n            console.log((write_count / element.articles_count * 100).toFixed(2) + \"%\");\n        });\n    });\n    zhihu.getAll();\n};\n\nrun(\"./\", \"YJango\");\n```\n\n### 事件\u0026\u0026方法\n\n本模块自定义的事件，都有对应的方法。\n\n|事件名|方法名|作用|\n|---|---|---|\n| info | onInfo | 获取专栏信息 |\n| batch_data |onData| 获取专栏数据，接受一个回调函数 |\n| done|onDone| 监听完成事件|\n\n## 使用的模块\n\n`lodash`：最好用的工具\n\n`turndown`：用于将 HTML 转成 Markdown\n\n`filenamify`: 解决 windows 文件命名错误问题\n\n[`zhihu-api`](https://github.com/bubao/zhihu-api): 自己封装和维护的知乎 api 模块\n\n## History\n\n## 2020-09-16 17:12:30\n\n- 使用 [zhihu-api v0.1.1](https://github.com/bubao/zhihu-api/tree/v0.1.1)\n- 事件监听改为`batch_data`\n- 原先的`filename`改为`filenameTime`，`filenameTime`为时间+文件名，原先的`filename`只是文件名\n\n### 2019-12-2 2:13:09\n\n使用 [zhihu-api v0.1.0](https://github.com/bubao/zhihu-api/tree/v0.1.0)，事件监听的方式获取专栏数据\n\n### 2019-4-9 2:29:32\n\n将模块迁移到知乎 api，只剩下知乎专栏爬虫，添加完整的 demo\n\n### 2019-1-6 22:39:57\n\n知乎 API 更新，重写部分代码。\n\n### 2018-11-22 22:21:40\n\n重构代码，只保留模块，代码的测试在`test`文件夹下。\n\n### 2018-09-13 16:59:46\n\n这次封装为模块，可二次开发。\n\n### 2018-5-15 18:21:05\n\n如今因为知乎 api 和知乎专栏的网页布局有所改变，现在重写了这个爬虫，api 模块使用的是 [zhihu](https://www.npmjs.com/package/zhihu) 的重构代码，模块中的 request 模块再次二次封装。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbubao%2Fzhihu-zhuanlan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbubao%2Fzhihu-zhuanlan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbubao%2Fzhihu-zhuanlan/lists"}