This commit is contained in:
Ubuntu
2026-07-03 16:58:28 +08:00
commit 32b43f85bd
6240 changed files with 1987678 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
# 依赖
node_modules/
# Virtual environments
.venv/
.venv-*/
# 数据文件
data/*.json
# 日志
*.log
# 环境变量
.env
# IDE
.vscode/
.idea/
+109
View File
@@ -0,0 +1,109 @@
# Playwright Hotspots - 中文平台热点抓取
使用 Puppeteer/Playwright 无头浏览器抓取微博、B站、知乎等平台的热点数据。
## 项目结构
```
playwright-hotspots/
├── README.md
├── package.json
├── config/
│ └── browser.js # 浏览器配置
├── src/
│ ├── platforms/
│ │ ├── weibo.js # 微博热搜
│ │ ├── bilibili.js # B站热搜
│ │ ├── zhihu.js # 知乎热榜
│ │ └── xiaohongshu.js # 小红书搜索
│ └── utils/
│ └── helpers.js # 工具函数
├── scripts/
│ ├── get-weibo-hot.js # 获取微博热搜
│ ├── get-bilibili-trending.js # 获取B站热搜
│ └── get-all-hotspots.js # 获取所有平台热点
└── data/ # 输出数据目录
```
## 安装浏览器
### 方式1: 自动安装(推荐,但较慢)
```bash
npx puppeteer browsers install chrome
```
### 方式2: 系统 Chromium
```bash
# Ubuntu/Debian
sudo apt-get install chromium-browser
# 然后在 config/browser.js 中设置
executablePath: '/usr/bin/chromium-browser'
```
### 方式3: 使用已安装的 Chrome
在 `config/browser.js` 中修改 `executablePath` 指向你的 Chrome 路径。
## 使用
```bash
# 获取微博热搜
node scripts/get-weibo-hot.js
# 获取B站热搜
node scripts/get-bilibili-trending.js
# 获取所有平台热点
node scripts/get-all-hotspots.js
```
## 快速开始
### 方式1: 使用全局命令(推荐)
```bash
# 已在 ~/.local/bin/hotspots 创建快捷命令
hotspots weibo # 微博热搜
hotspots bilibili # B站热搜
hotspots zhihu # 知乎热榜
hotspots all # 所有平台
hotspots data # 查看历史数据
```
### 方式2: 在项目目录运行
```bash
cd /home/ubuntu/nanocode/playwright-hotspots
# 检查浏览器环境
node scripts/check-browser.js
# 运行抓取
npm run weibo
npm run bilibili
npm run zhihu
npm run all
```
### 关于系统服务
本项目**不配置为系统服务**,按需手动运行更灵活。
如需定时抓取,可配置 cron 或使用 `hotspots all` 命令。
## 支持平台
| 平台 | 功能 | 状态 | 说明 |
|------|------|------|------|
| 微博 | 热搜榜 | ✅ 可用 | 网页抓取,无需登录 |
| B站 | 热搜榜 | ✅ 可用 | 直接调用 API |
| 知乎 | 热榜 | ✅ 可用 | 直接调用 API |
| 小红书 | 搜索 | ⚠️ 需登录 | 需要 Cookie/登录态 |
## 输出格式
```json
{
"platform": "weibo",
"timestamp": "2026-03-26T17:00:00+08:00",
"items": [
{"rank": 1, "title": "热点标题", "hot": "123万", "url": "..."}
]
}
```
+105
View File
@@ -0,0 +1,105 @@
/**
* 浏览器配置
* 支持 Puppeteer 和 Playwright
*/
const fs = require('fs');
// 浏览器可执行路径配置
const browserPaths = {
// Linux
linux: {
chromium: '/usr/bin/chromium-browser', // 优先使用系统 chromium
chromiumAlt: '/usr/bin/chromium',
chrome: '/usr/bin/google-chrome',
snapChromium: '/snap/bin/chromium',
},
// macOS
darwin: {
chrome: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
chromium: '/Applications/Chromium.app/Contents/MacOS/Chromium',
},
// Windows
win32: {
chrome: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
chromium: 'C:\\Program Files\\Chromium\\Application\\chromium.exe',
}
};
// 获取当前平台的浏览器路径
function getBrowserPath() {
const platform = process.platform;
const paths = browserPaths[platform] || browserPaths.linux;
// 按优先级返回所有可能的路径
return [
process.env.PUPPETEER_EXECUTABLE_PATH,
process.env.CHROME_BIN,
paths.chromium,
paths.chromiumAlt,
paths.chrome,
'/usr/bin/chromium',
].filter(Boolean);
}
// 查找第一个可用的浏览器
function findAvailableBrowser() {
const paths = getBrowserPath();
for (const p of paths) {
if (fs.existsSync(p)) {
return p;
}
}
return null;
}
// 浏览器启动配置 (Puppeteer)
function getLaunchOptions() {
const executablePath = findAvailableBrowser();
if (!executablePath) {
throw new Error('未找到可用的浏览器,请安装 Chromium 或 Chrome');
}
return {
headless: 'new', // 新的 headless 模式
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--disable-gpu',
'--window-size=1920,1080',
'--disable-blink-features=AutomationControlled', // 隐藏自动化特征
],
ignoreDefaultArgs: ['--enable-automation'], // 移除自动化标志
executablePath,
timeout: 30000,
};
}
// Playwright 配置
function getPlaywrightOptions() {
const executablePath = findAvailableBrowser();
if (!executablePath) {
throw new Error('未找到可用的浏览器,请安装 Chromium 或 Chrome');
}
return {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
],
executablePath,
};
}
module.exports = {
getLaunchOptions,
getPlaywrightOptions,
getBrowserPath,
findAvailableBrowser,
};
+982
View File
@@ -0,0 +1,982 @@
{
"name": "playwright-hotspots",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "playwright-hotspots",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"playwright": "^1.58.2",
"puppeteer-core": "^24.40.0"
}
},
"node_modules/@puppeteer/browsers": {
"version": "2.13.0",
"resolved": "https://registry.npmjs.org/@puppeteer/browsers/-/browsers-2.13.0.tgz",
"integrity": "sha512-46BZJYJjc/WwmKjsvDFykHtXrtomsCIrwYQPOP7VfMJoZY2bsDF9oROBABR3paDjDcmkUye1Pb1BqdcdiipaWA==",
"license": "Apache-2.0",
"dependencies": {
"debug": "^4.4.3",
"extract-zip": "^2.0.1",
"progress": "^2.0.3",
"proxy-agent": "^6.5.0",
"semver": "^7.7.4",
"tar-fs": "^3.1.1",
"yargs": "^17.7.2"
},
"bin": {
"browsers": "lib/cjs/main-cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/@tootallnate/quickjs-emscripten": {
"version": "0.23.0",
"resolved": "https://registry.npmjs.org/@tootallnate/quickjs-emscripten/-/quickjs-emscripten-0.23.0.tgz",
"integrity": "sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA==",
"license": "MIT"
},
"node_modules/@types/node": {
"version": "25.5.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.5.0.tgz",
"integrity": "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw==",
"license": "MIT",
"optional": true,
"dependencies": {
"undici-types": "~7.18.0"
}
},
"node_modules/@types/yauzl": {
"version": "2.10.3",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.3.tgz",
"integrity": "sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q==",
"license": "MIT",
"optional": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/agent-base": {
"version": "7.1.4",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz",
"integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==",
"license": "MIT",
"engines": {
"node": ">= 14"
}
},
"node_modules/ansi-regex": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
"integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"license": "MIT",
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/ast-types": {
"version": "0.13.4",
"resolved": "https://registry.npmjs.org/ast-types/-/ast-types-0.13.4.tgz",
"integrity": "sha512-x1FCFnFifvYDDzTaLII71vG5uvDwgtmDTEVWAxrgeiR8VjMONcCXJx7E+USjDtHlwFmt9MysbqgF9b9Vjr6w+w==",
"license": "MIT",
"dependencies": {
"tslib": "^2.0.1"
},
"engines": {
"node": ">=4"
}
},
"node_modules/b4a": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/b4a/-/b4a-1.8.0.tgz",
"integrity": "sha512-qRuSmNSkGQaHwNbM7J78Wwy+ghLEYF1zNrSeMxj4Kgw6y33O3mXcQ6Ie9fRvfU/YnxWkOchPXbaLb73TkIsfdg==",
"license": "Apache-2.0",
"peerDependencies": {
"react-native-b4a": "*"
},
"peerDependenciesMeta": {
"react-native-b4a": {
"optional": true
}
}
},
"node_modules/bare-events": {
"version": "2.8.2",
"resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.8.2.tgz",
"integrity": "sha512-riJjyv1/mHLIPX4RwiK+oW9/4c3TEUeORHKefKAKnZ5kyslbN+HXowtbaVEqt4IMUB7OXlfixcs6gsFeo/jhiQ==",
"license": "Apache-2.0",
"peerDependencies": {
"bare-abort-controller": "*"
},
"peerDependenciesMeta": {
"bare-abort-controller": {
"optional": true
}
}
},
"node_modules/bare-fs": {
"version": "4.5.6",
"resolved": "https://registry.npmjs.org/bare-fs/-/bare-fs-4.5.6.tgz",
"integrity": "sha512-1QovqDrR80Pmt5HPAsMsXTCFcDYr+NSUKW6nd6WO5v0JBmnItc/irNRzm2KOQ5oZ69P37y+AMujNyNtG+1Rggw==",
"license": "Apache-2.0",
"dependencies": {
"bare-events": "^2.5.4",
"bare-path": "^3.0.0",
"bare-stream": "^2.6.4",
"bare-url": "^2.2.2",
"fast-fifo": "^1.3.2"
},
"engines": {
"bare": ">=1.16.0"
},
"peerDependencies": {
"bare-buffer": "*"
},
"peerDependenciesMeta": {
"bare-buffer": {
"optional": true
}
}
},
"node_modules/bare-os": {
"version": "3.8.0",
"resolved": "https://registry.npmjs.org/bare-os/-/bare-os-3.8.0.tgz",
"integrity": "sha512-Dc9/SlwfxkXIGYhvMQNUtKaXCaGkZYGcd1vuNUUADVqzu4/vQfvnMkYYOUnt2VwQ2AqKr/8qAVFRtwETljgeFg==",
"license": "Apache-2.0",
"engines": {
"bare": ">=1.14.0"
}
},
"node_modules/bare-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/bare-path/-/bare-path-3.0.0.tgz",
"integrity": "sha512-tyfW2cQcB5NN8Saijrhqn0Zh7AnFNsnczRcuWODH0eYAXBsJ5gVxAUuNr7tsHSC6IZ77cA0SitzT+s47kot8Mw==",
"license": "Apache-2.0",
"dependencies": {
"bare-os": "^3.0.1"
}
},
"node_modules/bare-stream": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/bare-stream/-/bare-stream-2.11.0.tgz",
"integrity": "sha512-Y/+iQ49fL3rIn6w/AVxI/2+BRrpmzJvdWt5Jv8Za6Ngqc6V227c+pYjYYgLdpR3MwQ9ObVXD0ZrqoBztakM0rw==",
"license": "Apache-2.0",
"dependencies": {
"streamx": "^2.25.0",
"teex": "^1.0.1"
},
"peerDependencies": {
"bare-abort-controller": "*",
"bare-buffer": "*",
"bare-events": "*"
},
"peerDependenciesMeta": {
"bare-abort-controller": {
"optional": true
},
"bare-buffer": {
"optional": true
},
"bare-events": {
"optional": true
}
}
},
"node_modules/bare-url": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/bare-url/-/bare-url-2.4.0.tgz",
"integrity": "sha512-NSTU5WN+fy/L0DDenfE8SXQna4voXuW0FHM7wH8i3/q9khUSchfPbPezO4zSFMnDGIf9YE+mt/RWhZgNRKRIXA==",
"license": "Apache-2.0",
"dependencies": {
"bare-path": "^3.0.0"
}
},
"node_modules/basic-ftp": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/basic-ftp/-/basic-ftp-5.2.0.tgz",
"integrity": "sha512-VoMINM2rqJwJgfdHq6RiUudKt2BV+FY5ZFezP/ypmwayk68+NzzAQy4XXLlqsGD4MCzq3DrmNFD/uUmBJuGoXw==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/buffer-crc32": {
"version": "0.2.13",
"resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz",
"integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==",
"license": "MIT",
"engines": {
"node": "*"
}
},
"node_modules/chromium-bidi": {
"version": "14.0.0",
"resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-14.0.0.tgz",
"integrity": "sha512-9gYlLtS6tStdRWzrtXaTMnqcM4dudNegMXJxkR0I/CXObHalYeYcAMPrL19eroNZHtJ8DQmu1E+ZNOYu/IXMXw==",
"license": "Apache-2.0",
"dependencies": {
"mitt": "^3.0.1",
"zod": "^3.24.1"
},
"peerDependencies": {
"devtools-protocol": "*"
}
},
"node_modules/cliui": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",
"integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==",
"license": "ISC",
"dependencies": {
"string-width": "^4.2.0",
"strip-ansi": "^6.0.1",
"wrap-ansi": "^7.0.0"
},
"engines": {
"node": ">=12"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"license": "MIT",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
},
"node_modules/data-uri-to-buffer": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-6.0.2.tgz",
"integrity": "sha512-7hvf7/GW8e86rW0ptuwS3OcBGDjIi6SZva7hCyWC0yYry2cOPmLIjXAUHI6DK2HsnwJd9ifmt57i8eV2n4YNpw==",
"license": "MIT",
"engines": {
"node": ">= 14"
}
},
"node_modules/debug": {
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
"license": "MIT",
"dependencies": {
"ms": "^2.1.3"
},
"engines": {
"node": ">=6.0"
},
"peerDependenciesMeta": {
"supports-color": {
"optional": true
}
}
},
"node_modules/degenerator": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/degenerator/-/degenerator-5.0.1.tgz",
"integrity": "sha512-TllpMR/t0M5sqCXfj85i4XaAzxmS5tVA16dqvdkMwGmzI+dXLXnw3J+3Vdv7VKw+ThlTMboK6i9rnZ6Nntj5CQ==",
"license": "MIT",
"dependencies": {
"ast-types": "^0.13.4",
"escodegen": "^2.1.0",
"esprima": "^4.0.1"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/devtools-protocol": {
"version": "0.0.1581282",
"resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1581282.tgz",
"integrity": "sha512-nv7iKtNZQshSW2hKzYNr46nM/Cfh5SEvE2oV0/SEGgc9XupIY5ggf84Cz8eJIkBce7S3bmTAauFD6aysMpnqsQ==",
"license": "BSD-3-Clause"
},
"node_modules/emoji-regex": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
"integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
"license": "MIT"
},
"node_modules/end-of-stream": {
"version": "1.4.5",
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz",
"integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==",
"license": "MIT",
"dependencies": {
"once": "^1.4.0"
}
},
"node_modules/escalade": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz",
"integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==",
"license": "MIT",
"engines": {
"node": ">=6"
}
},
"node_modules/escodegen": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/escodegen/-/escodegen-2.1.0.tgz",
"integrity": "sha512-2NlIDTwUWJN0mRPQOdtQBzbUHvdGY2P1VXSyU83Q3xKxM7WHX2Ql8dKq782Q9TgQUNOLEzEYu9bzLNj1q88I5w==",
"license": "BSD-2-Clause",
"dependencies": {
"esprima": "^4.0.1",
"estraverse": "^5.2.0",
"esutils": "^2.0.2"
},
"bin": {
"escodegen": "bin/escodegen.js",
"esgenerate": "bin/esgenerate.js"
},
"engines": {
"node": ">=6.0"
},
"optionalDependencies": {
"source-map": "~0.6.1"
}
},
"node_modules/esprima": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"license": "BSD-2-Clause",
"bin": {
"esparse": "bin/esparse.js",
"esvalidate": "bin/esvalidate.js"
},
"engines": {
"node": ">=4"
}
},
"node_modules/estraverse": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/estraverse/-/estraverse-5.3.0.tgz",
"integrity": "sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=4.0"
}
},
"node_modules/esutils": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz",
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/events-universal": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/events-universal/-/events-universal-1.0.1.tgz",
"integrity": "sha512-LUd5euvbMLpwOF8m6ivPCbhQeSiYVNb8Vs0fQ8QjXo0JTkEHpz8pxdQf0gStltaPpw0Cca8b39KxvK9cfKRiAw==",
"license": "Apache-2.0",
"dependencies": {
"bare-events": "^2.7.0"
}
},
"node_modules/extract-zip": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz",
"integrity": "sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==",
"license": "BSD-2-Clause",
"dependencies": {
"debug": "^4.1.1",
"get-stream": "^5.1.0",
"yauzl": "^2.10.0"
},
"bin": {
"extract-zip": "cli.js"
},
"engines": {
"node": ">= 10.17.0"
},
"optionalDependencies": {
"@types/yauzl": "^2.9.1"
}
},
"node_modules/fast-fifo": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz",
"integrity": "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==",
"license": "MIT"
},
"node_modules/fd-slicer": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz",
"integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==",
"license": "MIT",
"dependencies": {
"pend": "~1.2.0"
}
},
"node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/get-caller-file": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
"integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
"license": "ISC",
"engines": {
"node": "6.* || 8.* || >= 10.*"
}
},
"node_modules/get-stream": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz",
"integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==",
"license": "MIT",
"dependencies": {
"pump": "^3.0.0"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/get-uri": {
"version": "6.0.5",
"resolved": "https://registry.npmjs.org/get-uri/-/get-uri-6.0.5.tgz",
"integrity": "sha512-b1O07XYq8eRuVzBNgJLstU6FYc1tS6wnMtF1I1D9lE8LxZSOGZ7LhxN54yPP6mGw5f2CkXY2BQUL9Fx41qvcIg==",
"license": "MIT",
"dependencies": {
"basic-ftp": "^5.0.2",
"data-uri-to-buffer": "^6.0.2",
"debug": "^4.3.4"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/http-proxy-agent": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-7.0.2.tgz",
"integrity": "sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig==",
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.0",
"debug": "^4.3.4"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/https-proxy-agent": {
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz",
"integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==",
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.2",
"debug": "4"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/ip-address": {
"version": "10.1.0",
"resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.1.0.tgz",
"integrity": "sha512-XXADHxXmvT9+CRxhXg56LJovE+bmWnEWB78LB83VZTprKTmaC5QfruXocxzTZ2Kl0DNwKuBdlIhjL8LeY8Sf8Q==",
"license": "MIT",
"engines": {
"node": ">= 12"
}
},
"node_modules/is-fullwidth-code-point": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
"integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/lru-cache": {
"version": "7.18.3",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.18.3.tgz",
"integrity": "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==",
"license": "ISC",
"engines": {
"node": ">=12"
}
},
"node_modules/mitt": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.1.tgz",
"integrity": "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw==",
"license": "MIT"
},
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT"
},
"node_modules/netmask": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/netmask/-/netmask-2.0.2.tgz",
"integrity": "sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==",
"license": "MIT",
"engines": {
"node": ">= 0.4.0"
}
},
"node_modules/once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
"license": "ISC",
"dependencies": {
"wrappy": "1"
}
},
"node_modules/pac-proxy-agent": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/pac-proxy-agent/-/pac-proxy-agent-7.2.0.tgz",
"integrity": "sha512-TEB8ESquiLMc0lV8vcd5Ql/JAKAoyzHFXaStwjkzpOpC5Yv+pIzLfHvjTSdf3vpa2bMiUQrg9i6276yn8666aA==",
"license": "MIT",
"dependencies": {
"@tootallnate/quickjs-emscripten": "^0.23.0",
"agent-base": "^7.1.2",
"debug": "^4.3.4",
"get-uri": "^6.0.1",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.6",
"pac-resolver": "^7.0.1",
"socks-proxy-agent": "^8.0.5"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/pac-resolver": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/pac-resolver/-/pac-resolver-7.0.1.tgz",
"integrity": "sha512-5NPgf87AT2STgwa2ntRMr45jTKrYBGkVU36yT0ig/n/GMAa3oPqhZfIQ2kMEimReg0+t9kZViDVZ83qfVUlckg==",
"license": "MIT",
"dependencies": {
"degenerator": "^5.0.0",
"netmask": "^2.0.2"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/pend": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz",
"integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==",
"license": "MIT"
},
"node_modules/playwright": {
"version": "1.58.2",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.58.2.tgz",
"integrity": "sha512-vA30H8Nvkq/cPBnNw4Q8TWz1EJyqgpuinBcHET0YVJVFldr8JDNiU9LaWAE1KqSkRYazuaBhTpB5ZzShOezQ6A==",
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.58.2"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
},
"node_modules/playwright-core": {
"version": "1.58.2",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.58.2.tgz",
"integrity": "sha512-yZkEtftgwS8CsfYo7nm0KE8jsvm6i/PTgVtB8DL726wNf6H2IMsDuxCpJj59KDaxCtSnrWan2AeDqM7JBaultg==",
"license": "Apache-2.0",
"bin": {
"playwright-core": "cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/progress": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz",
"integrity": "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==",
"license": "MIT",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/proxy-agent": {
"version": "6.5.0",
"resolved": "https://registry.npmjs.org/proxy-agent/-/proxy-agent-6.5.0.tgz",
"integrity": "sha512-TmatMXdr2KlRiA2CyDu8GqR8EjahTG3aY3nXjdzFyoZbmB8hrBsTyMezhULIXKnC0jpfjlmiZ3+EaCzoInSu/A==",
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.2",
"debug": "^4.3.4",
"http-proxy-agent": "^7.0.1",
"https-proxy-agent": "^7.0.6",
"lru-cache": "^7.14.1",
"pac-proxy-agent": "^7.1.0",
"proxy-from-env": "^1.1.0",
"socks-proxy-agent": "^8.0.5"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
"license": "MIT"
},
"node_modules/pump": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz",
"integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==",
"license": "MIT",
"dependencies": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"
}
},
"node_modules/puppeteer-core": {
"version": "24.40.0",
"resolved": "https://registry.npmjs.org/puppeteer-core/-/puppeteer-core-24.40.0.tgz",
"integrity": "sha512-MWL3XbUCfVgGR0gRsidzT6oKJT2QydPLhMITU6HoVWiiv4gkb6gJi3pcdAa8q4HwjBTbqISOWVP4aJiiyUJvag==",
"license": "Apache-2.0",
"dependencies": {
"@puppeteer/browsers": "2.13.0",
"chromium-bidi": "14.0.0",
"debug": "^4.4.3",
"devtools-protocol": "0.0.1581282",
"typed-query-selector": "^2.12.1",
"webdriver-bidi-protocol": "0.4.1",
"ws": "^8.19.0"
},
"engines": {
"node": ">=18"
}
},
"node_modules/require-directory": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
"integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/semver": {
"version": "7.7.4",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz",
"integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==",
"license": "ISC",
"bin": {
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/smart-buffer": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz",
"integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==",
"license": "MIT",
"engines": {
"node": ">= 6.0.0",
"npm": ">= 3.0.0"
}
},
"node_modules/socks": {
"version": "2.8.7",
"resolved": "https://registry.npmjs.org/socks/-/socks-2.8.7.tgz",
"integrity": "sha512-HLpt+uLy/pxB+bum/9DzAgiKS8CX1EvbWxI4zlmgGCExImLdiad2iCwXT5Z4c9c3Eq8rP2318mPW2c+QbtjK8A==",
"license": "MIT",
"dependencies": {
"ip-address": "^10.0.1",
"smart-buffer": "^4.2.0"
},
"engines": {
"node": ">= 10.0.0",
"npm": ">= 3.0.0"
}
},
"node_modules/socks-proxy-agent": {
"version": "8.0.5",
"resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-8.0.5.tgz",
"integrity": "sha512-HehCEsotFqbPW9sJ8WVYB6UbmIMv7kUUORIF2Nncq4VQvBfNBLibW9YZR5dlYCSUhwcD628pRllm7n+E+YTzJw==",
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.2",
"debug": "^4.3.4",
"socks": "^2.8.3"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"license": "BSD-3-Clause",
"optional": true,
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/streamx": {
"version": "2.25.0",
"resolved": "https://registry.npmjs.org/streamx/-/streamx-2.25.0.tgz",
"integrity": "sha512-0nQuG6jf1w+wddNEEXCF4nTg3LtufWINB5eFEN+5TNZW7KWJp6x87+JFL43vaAUPyCfH1wID+mNVyW6OHtFamg==",
"license": "MIT",
"dependencies": {
"events-universal": "^1.0.0",
"fast-fifo": "^1.3.2",
"text-decoder": "^1.1.0"
}
},
"node_modules/string-width": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
"integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
"license": "MIT",
"dependencies": {
"emoji-regex": "^8.0.0",
"is-fullwidth-code-point": "^3.0.0",
"strip-ansi": "^6.0.1"
},
"engines": {
"node": ">=8"
}
},
"node_modules/strip-ansi": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
"integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
"license": "MIT",
"dependencies": {
"ansi-regex": "^5.0.1"
},
"engines": {
"node": ">=8"
}
},
"node_modules/tar-fs": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-3.1.2.tgz",
"integrity": "sha512-QGxxTxxyleAdyM3kpFs14ymbYmNFrfY+pHj7Z8FgtbZ7w2//VAgLMac7sT6nRpIHjppXO2AwwEOg0bPFVRcmXw==",
"license": "MIT",
"dependencies": {
"pump": "^3.0.0",
"tar-stream": "^3.1.5"
},
"optionalDependencies": {
"bare-fs": "^4.0.1",
"bare-path": "^3.0.0"
}
},
"node_modules/tar-stream": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-3.1.8.tgz",
"integrity": "sha512-U6QpVRyCGHva435KoNWy9PRoi2IFYCgtEhq9nmrPPpbRacPs9IH4aJ3gbrFC8dPcXvdSZ4XXfXT5Fshbp2MtlQ==",
"license": "MIT",
"dependencies": {
"b4a": "^1.6.4",
"bare-fs": "^4.5.5",
"fast-fifo": "^1.2.0",
"streamx": "^2.15.0"
}
},
"node_modules/teex": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/teex/-/teex-1.0.1.tgz",
"integrity": "sha512-eYE6iEI62Ni1H8oIa7KlDU6uQBtqr4Eajni3wX7rpfXD8ysFx8z0+dri+KWEPWpBsxXfxu58x/0jvTVT1ekOSg==",
"license": "MIT",
"dependencies": {
"streamx": "^2.12.5"
}
},
"node_modules/text-decoder": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/text-decoder/-/text-decoder-1.2.7.tgz",
"integrity": "sha512-vlLytXkeP4xvEq2otHeJfSQIRyWxo/oZGEbXrtEEF9Hnmrdly59sUbzZ/QgyWuLYHctCHxFF4tRQZNQ9k60ExQ==",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.4"
}
},
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
"license": "0BSD"
},
"node_modules/typed-query-selector": {
"version": "2.12.1",
"resolved": "https://registry.npmjs.org/typed-query-selector/-/typed-query-selector-2.12.1.tgz",
"integrity": "sha512-uzR+FzI8qrUEIu96oaeBJmd9E7CFEiQ3goA5qCVgc4s5llSubcfGHq9yUstZx/k4s9dXHVKsE35YWoFyvEqEHA==",
"license": "MIT"
},
"node_modules/undici-types": {
"version": "7.18.2",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz",
"integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==",
"license": "MIT",
"optional": true
},
"node_modules/webdriver-bidi-protocol": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/webdriver-bidi-protocol/-/webdriver-bidi-protocol-0.4.1.tgz",
"integrity": "sha512-ARrjNjtWRRs2w4Tk7nqrf2gBI0QXWuOmMCx2hU+1jUt6d00MjMxURrhxhGbrsoiZKJrhTSTzbIrc554iKI10qw==",
"license": "Apache-2.0"
},
"node_modules/wrap-ansi": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz",
"integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==",
"license": "MIT",
"dependencies": {
"ansi-styles": "^4.0.0",
"string-width": "^4.1.0",
"strip-ansi": "^6.0.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/wrap-ansi?sponsor=1"
}
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"license": "ISC"
},
"node_modules/ws": {
"version": "8.20.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.20.0.tgz",
"integrity": "sha512-sAt8BhgNbzCtgGbt2OxmpuryO63ZoDk/sqaB/znQm94T4fCEsy/yV+7CdC1kJhOU9lboAEU7R3kquuycDoibVA==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/y18n": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
"integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==",
"license": "ISC",
"engines": {
"node": ">=10"
}
},
"node_modules/yargs": {
"version": "17.7.2",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
"integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==",
"license": "MIT",
"dependencies": {
"cliui": "^8.0.1",
"escalade": "^3.1.1",
"get-caller-file": "^2.0.5",
"require-directory": "^2.1.1",
"string-width": "^4.2.3",
"y18n": "^5.0.5",
"yargs-parser": "^21.1.1"
},
"engines": {
"node": ">=12"
}
},
"node_modules/yargs-parser": {
"version": "21.1.1",
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz",
"integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==",
"license": "ISC",
"engines": {
"node": ">=12"
}
},
"node_modules/yauzl": {
"version": "2.10.0",
"resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz",
"integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==",
"license": "MIT",
"dependencies": {
"buffer-crc32": "~0.2.3",
"fd-slicer": "~1.1.0"
}
},
"node_modules/zod": {
"version": "3.25.76",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz",
"integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
}
}
}
+24
View File
@@ -0,0 +1,24 @@
{
"name": "playwright-hotspots",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"weibo": "node scripts/get-weibo-hot.js",
"bilibili": "node scripts/get-bilibili-trending.js",
"zhihu": "node scripts/get-zhihu-hot.js",
"juejin": "node scripts/get-juejin-hot.js",
"cnblogs": "node scripts/get-cnblogs-hot.js",
"tech": "node scripts/get-tech-hot.js 2>/dev/null || echo '请运行: hotspots tech'",
"all": "node scripts/get-all-hotspots.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"playwright": "^1.58.2",
"puppeteer-core": "^24.40.0"
}
}
@@ -0,0 +1,28 @@
#!/usr/bin/env node
/**
* 检查浏览器环境
*/
const fs = require('fs');
const { getBrowserPath } = require('../config/browser');
console.log('🔍 检查浏览器环境...\n');
const paths = getBrowserPath();
console.log('可能的浏览器路径:');
paths.forEach((p, i) => {
const exists = fs.existsSync(p);
console.log(` ${i + 1}. ${p} ${exists ? '✅' : '❌'}`);
});
const found = paths.find(p => fs.existsSync(p));
if (found) {
console.log(`\n✅ 找到可用浏览器: ${found}`);
process.exit(0);
} else {
console.log('\n❌ 未找到浏览器,请安装:');
console.log(' 方式1: npx puppeteer browsers install chrome');
console.log(' 方式2: sudo apt-get install chromium-browser');
console.log(' 方式3: 设置环境变量 PUPPETEER_EXECUTABLE_PATH=/path/to/chrome');
process.exit(1);
}
@@ -0,0 +1,64 @@
#!/usr/bin/env node
/**
* 获取所有平台热点
*/
const { fetchWeiboHot } = require('../src/platforms/weibo');
const { fetchBilibiliTrending } = require('../src/platforms/bilibili');
const { fetchZhihuHot } = require('../src/platforms/zhihu');
async function getAllHotspots() {
console.log('='.repeat(80));
console.log('🔥 中文平台热点聚合抓取');
console.log('='.repeat(80));
const results = {
timestamp: new Date().toISOString(),
platforms: {},
};
// 微博
try {
console.log('\n📌 1. 微博热搜');
results.platforms.weibo = await fetchWeiboHot();
} catch (e) {
console.error('微博抓取失败:', e.message);
results.platforms.weibo = { error: e.message };
}
// B站
try {
console.log('\n📌 2. B站热搜');
results.platforms.bilibili = await fetchBilibiliTrending();
} catch (e) {
console.error('B站抓取失败:', e.message);
results.platforms.bilibili = { error: e.message };
}
// 知乎
try {
console.log('\n📌 3. 知乎热榜');
results.platforms.zhihu = await fetchZhihuHot();
} catch (e) {
console.error('知乎抓取失败:', e.message);
results.platforms.zhihu = { error: e.message };
}
console.log('\n' + '='.repeat(80));
console.log('✅ 所有平台抓取完成');
console.log('='.repeat(80));
// 保存汇总
const fs = require('fs');
const path = require('path');
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filepath = path.join(__dirname, '../data', `all_hotspots_${timestamp}.json`);
fs.mkdirSync(path.dirname(filepath), { recursive: true });
fs.writeFileSync(filepath, JSON.stringify(results, null, 2), 'utf-8');
console.log(`💾 汇总数据已保存: ${filepath}`);
return results;
}
getAllHotspots().catch(console.error);
@@ -0,0 +1,6 @@
#!/usr/bin/env node
/**
* 获取B站热搜
*/
const { fetchBilibiliTrending } = require('../src/platforms/bilibili');
fetchBilibiliTrending().catch(console.error);
@@ -0,0 +1,11 @@
#!/usr/bin/env node
/**
* 获取博客园热门文章
*/
const { fetchCnblogsHot, searchCnblogs } = require('../src/platforms/techblogs/cnblogs');
const keyword = process.argv[2];
if (keyword) {
searchCnblogs(keyword).catch(console.error);
} else {
fetchCnblogsHot().catch(console.error);
}
@@ -0,0 +1,7 @@
#!/usr/bin/env node
/**
* 获取掘金热门文章
*/
const { fetchJuejinHot } = require('../src/platforms/techblogs/juejin');
const category = process.argv[2] || 'all';
fetchJuejinHot(category).catch(console.error);
@@ -0,0 +1,6 @@
#!/usr/bin/env node
/**
* 获取微博热搜
*/
const { fetchWeiboHot } = require('../src/platforms/weibo');
fetchWeiboHot().catch(console.error);
@@ -0,0 +1,6 @@
#!/usr/bin/env node
/**
* 获取知乎热榜
*/
const { fetchZhihuHot } = require('../src/platforms/zhihu');
fetchZhihuHot().catch(console.error);
@@ -0,0 +1,211 @@
#!/usr/bin/env node
/**
* 使用 Puppeteer 搜索技术博客
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const path = require('path');
function saveResult(platform, keyword, items) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const data = {
platform,
keyword,
timestamp: new Date().toISOString(),
count: items.length,
items,
};
const filepath = path.join(__dirname, '../data', `${platform}_search_${timestamp}.json`);
if (!fs.existsSync(path.dirname(filepath))) {
fs.mkdirSync(path.dirname(filepath), { recursive: true });
}
fs.writeFileSync(filepath, JSON.stringify(data, null, 2), 'utf-8');
return filepath;
}
function printResults(items, platform) {
console.log('\n' + '='.repeat(70));
items.forEach((item, i) => {
console.log(`${i + 1}. ${item.title.substring(0, 60)}${item.title.length > 60 ? '...' : ''}`);
console.log(` ${item.url}`);
});
console.log('='.repeat(70));
}
async function searchJuejin(browser, keyword) {
console.log(`\n📱 搜索掘金: "${keyword}"`);
const page = await browser.newPage();
try {
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
const url = `https://juejin.cn/search?query=${encodeURIComponent(keyword)}&type=0`;
await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
// 等待搜索结果加载
await new Promise(r => setTimeout(r, 3000));
// 提取搜索结果
const items = await page.evaluate(() => {
const results = [];
// 掘金搜索结果选择器
const selectors = [
'.search-result-list .item a.title',
'.result-list .item .title a',
'[class*="search"] [class*="item"] a[href*="/post/"]',
'.article-item a',
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
if (elements.length > 0) {
elements.forEach((el) => {
const title = el.textContent?.trim();
const href = el.getAttribute('href');
if (title && href && title.length > 5 && !results.find(r => r.title === title)) {
const articleId = href.match(/\/post\/(\d+)/)?.[1];
if (articleId) {
results.push({
title: title.substring(0, 100),
url: `https://juejin.cn/post/${articleId}`,
});
}
}
});
break;
}
}
return results.slice(0, 10);
});
if (items.length > 0) {
printResults(items, '掘金');
const filepath = saveResult('juejin', keyword, items);
console.log(`✅ 找到 ${items.length} 条结果`);
console.log(`💾 已保存: ${filepath}`);
} else {
console.log('⚠️ 未找到结果,页面可能需要登录');
}
return items;
} catch (e) {
console.error('❌ 掘金搜索失败:', e.message);
return [];
} finally {
await page.close();
}
}
async function searchCnblogs(browser, keyword) {
console.log(`\n💻 搜索博客园: "${keyword}"`);
const page = await browser.newPage();
try {
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
const url = `https://zzk.cnblogs.com/s?w=${encodeURIComponent(keyword)}`;
await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 60000,
});
await new Promise(r => setTimeout(r, 3000));
// 提取搜索结果
const items = await page.evaluate(() => {
const results = [];
const selectors = [
'.searchItem .searchItemTitle a',
'.search-item .title a',
'#search_result .searchItem a',
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
if (elements.length > 0) {
elements.forEach((el) => {
const title = el.textContent?.trim();
const href = el.getAttribute('href');
if (title && href && title.length > 5 && !results.find(r => r.title === title)) {
results.push({
title: title.substring(0, 100),
url: href.startsWith('http') ? href : `https:${href}`,
});
}
});
break;
}
}
return results.slice(0, 10);
});
if (items.length > 0) {
printResults(items, '博客园');
const filepath = saveResult('cnblogs', keyword, items);
console.log(`✅ 找到 ${items.length} 条结果`);
console.log(`💾 已保存: ${filepath}`);
} else {
console.log('⚠️ 未找到结果');
}
return items;
} catch (e) {
console.error('❌ 博客园搜索失败:', e.message);
return [];
} finally {
await page.close();
}
}
async function main() {
const keyword = process.argv[2] || 'android 开发';
const platform = process.argv[3] || 'all';
console.log('='.repeat(70));
console.log(`🔍 技术博客搜索: "${keyword}"`);
console.log('='.repeat(70));
const browser = await puppeteer.launch(getLaunchOptions());
try {
if (platform === 'all' || platform === 'juejin') {
await searchJuejin(browser, keyword);
}
if (platform === 'all' || platform === 'cnblogs') {
await searchCnblogs(browser, keyword);
}
} finally {
await browser.close();
}
console.log('\n' + '='.repeat(70));
console.log('搜索完成');
console.log('='.repeat(70));
}
main().catch(console.error);
+149
View File
@@ -0,0 +1,149 @@
#!/usr/bin/env node
/**
* 搜索技术博客
* 用法: node search-tech.js <关键词> [juejin|cnblogs|all]
*/
const https = require('https');
const fs = require('fs');
const path = require('path');
// 保存结果
function saveResult(platform, data) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `${platform}_search_${timestamp}.json`;
const filepath = path.join(__dirname, '../data', filename);
if (!fs.existsSync(path.dirname(filepath))) {
fs.mkdirSync(path.dirname(filepath), { recursive: true });
}
fs.writeFileSync(filepath, JSON.stringify(data, null, 2), 'utf-8');
console.log(`💾 结果已保存: ${filepath}`);
}
// HTTP 请求
function httpGet(url, headers = {}) {
return new Promise((resolve, reject) => {
https.get(url, { headers }, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve(data));
}).on('error', reject);
});
}
// 搜索掘金
async function searchJuejin(keyword) {
console.log(`\n📱 搜索掘金: "${keyword}"`);
console.log('-'.repeat(60));
try {
const url = `https://juejin.cn/search?query=${encodeURIComponent(keyword)}&type=0`;
const html = await httpGet(url, {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
'Referer': 'https://juejin.cn/',
});
const items = [];
const regex = /href="https:\/\/juejin\.cn\/post\/(\d+)"[^>]*>([^<]+)<\/a>/gi;
let match;
while ((match = regex.exec(html)) !== null && items.length < 10) {
const title = match[2].trim();
if (title.length > 5 && !items.find(i => i.title === title)) {
items.push({
rank: items.length + 1,
title: title.substring(0, 80),
url: `https://juejin.cn/post/${match[1]}`,
});
}
}
if (items.length > 0) {
items.forEach(i => console.log(`${i.rank}. ${i.title}`));
saveResult('juejin', { keyword, items });
console.log(`✅ 找到 ${items.length} 条结果`);
} else {
console.log('⚠️ 未找到结果');
}
return items;
} catch (e) {
console.error('❌ 掘金搜索失败:', e.message);
return [];
}
}
// 搜索博客园
async function searchCnblogs(keyword) {
console.log(`\n💻 搜索博客园: "${keyword}"`);
console.log('-'.repeat(60));
try {
const url = `https://zzk.cnblogs.com/s?w=${encodeURIComponent(keyword)}`;
const html = await httpGet(url, {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
});
const items = [];
// 匹配搜索结果
const titleRegex = /<a[^>]*class="searchItemTitle"[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/gi;
let match;
while ((match = titleRegex.exec(html)) !== null && items.length < 10) {
const url = match[1];
const title = match[2].replace(/<[^>]+>/g, '').trim();
if (title.length > 5 && !items.find(i => i.title === title)) {
items.push({
rank: items.length + 1,
title: title.substring(0, 80),
url: url.startsWith('http') ? url : `https:${url}`,
});
}
}
if (items.length > 0) {
items.forEach(i => console.log(`${i.rank}. ${i.title}`));
saveResult('cnblogs', { keyword, items });
console.log(`✅ 找到 ${items.length} 条结果`);
} else {
console.log('⚠️ 未找到结果');
}
return items;
} catch (e) {
console.error('❌ 博客园搜索失败:', e.message);
return [];
}
}
// 主函数
async function main() {
const keyword = process.argv[2];
const platform = process.argv[3] || 'all';
if (!keyword) {
console.log('用法: node search-tech.js <关键词> [juejin|cnblogs|all]');
process.exit(1);
}
console.log('='.repeat(60));
console.log(`🔍 技术博客搜索: "${keyword}"`);
console.log('='.repeat(60));
if (platform === 'all' || platform === 'juejin') {
await searchJuejin(keyword);
}
if (platform === 'all' || platform === 'cnblogs') {
await searchCnblogs(keyword);
}
console.log('\n' + '='.repeat(60));
console.log('搜索完成');
console.log('='.repeat(60));
}
main().catch(console.error);
@@ -0,0 +1,77 @@
/**
* 检查微博发送状态
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const COOKIE_FILE = '/home/ubuntu/weibo_cookies.txt';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function checkStatus() {
console.log('🔍 检查发送状态...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 加载 Cookie
const cookies = JSON.parse(fs.readFileSync(COOKIE_FILE, 'utf-8'));
await page.goto('https://weibo.com', { waitUntil: 'domcontentloaded' });
for (const c of cookies) {
try {
await page.setCookie({
name: c.name,
value: c.value,
domain: c.domain,
path: c.path || '/'
});
} catch (e) {}
}
// 访问个人主页查看最新微博
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
await delay(3000);
await page.screenshot({ path: '/tmp/weibo_login/check_status.png' });
// 获取最新的几条微博
const posts = await page.evaluate(() => {
const items = Array.from(document.querySelectorAll('[class*="card"], [class*="item"], article'));
return items.slice(0, 3).map(item => {
const text = item.textContent || '';
const time = item.querySelector('time, [class*="time"], [class*="date"]')?.textContent || '';
return { text: text.substring(0, 200), time };
});
});
console.log('\n📱 最新微博内容:');
posts.forEach((post, i) => {
console.log(`\n[${i + 1}] ${post.time}`);
console.log(` ${post.text.substring(0, 100)}...`);
});
// 检查是否有刚刚发的测试微博
const hasTestPost = posts.some(p => p.text.includes('自动化脚本') || p.text.includes('🤖'));
if (hasTestPost) {
console.log('\n✅ 找到了测试微博!');
} else {
console.log('\n⚠️ 未找到测试微博,可能发送失败了');
}
} catch (error) {
console.error('❌ 错误:', error.message);
} finally {
await browser.close();
}
}
checkStatus().catch(console.error);
+159
View File
@@ -0,0 +1,159 @@
/**
* 微博自动发布 - 最终版本
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const COOKIE_FILE = '/home/ubuntu/weibo_cookies.txt';
const TEST_CONTENT = '🤖 这是一条来自自动化脚本的测试微博\n时间:' + new Date().toLocaleString('zh-CN') + '\n#自动化测试 #机器人';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function postWeibo() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 加载 Cookie
console.log('🍪 加载 Cookie...');
const cookies = JSON.parse(fs.readFileSync(COOKIE_FILE, 'utf-8'));
await page.goto('https://weibo.com', { waitUntil: 'domcontentloaded' });
for (const c of cookies) {
try {
await page.setCookie({
name: c.name,
value: c.value,
domain: c.domain,
path: c.path || '/',
secure: c.secure || false,
httpOnly: c.httpOnly || false
});
} catch (e) {}
}
// 访问主页
console.log('🔄 访问微博主页...');
await page.goto('https://weibo.com', { waitUntil: 'networkidle2' });
await delay(3000);
await page.screenshot({ path: '/tmp/weibo_login/step1_home.png' });
// 查找并点击"写微博"按钮
console.log('🖱️ 查找"写微博"按钮...');
const writeBtn = await page.$eval('button', buttons => {
const btns = Array.from(document.querySelectorAll('button'));
return btns.find(b => b.textContent?.includes('写微博'));
}).catch(() => null);
// 通过文本查找
let found = false;
const allBtns = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('写微博')) {
console.log(` 找到: "${text.trim()}"`);
await btn.click();
found = true;
break;
}
}
if (!found) {
console.log('⚠️ 未找到"写微博"按钮,尝试直接访问compose页面');
await page.goto('https://weibo.com/compose', { waitUntil: 'networkidle2' });
} else {
await delay(2000);
}
await page.screenshot({ path: '/tmp/weibo_login/step2_compose.png' });
// 查找发布框
console.log('📝 查找发布框...');
const selectors = [
'textarea[placeholder*="有什么新鲜事"]',
'textarea[placeholder*="想说什么"]',
'textarea.woo-input-main',
'[class*="input"] textarea',
'textarea',
];
let postBox = null;
for (const sel of selectors) {
postBox = await page.$(sel);
if (postBox) {
console.log(` 找到: ${sel}`);
break;
}
}
if (!postBox) {
// 获取所有textarea
const textareas = await page.$$('textarea');
console.log(` 尝试 ${textareas.length} 个 textarea`);
if (textareas.length > 0) {
postBox = textareas[0];
}
}
if (postBox) {
// 输入内容
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT.substring(0, 50) + '...');
await delay(1000);
await page.screenshot({ path: '/tmp/weibo_login/step3_typed.png' });
// 查找发送按钮
console.log('🔍 查找发送按钮...');
let sendBtn = null;
const allBtns2 = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns2) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('发送') || text?.includes('发布')) {
console.log(` 找到: "${text.trim()}"`);
sendBtn = btn;
break;
}
}
if (sendBtn) {
console.log('🚀 点击发送...');
await sendBtn.click();
await delay(5000);
await page.screenshot({ path: '/tmp/weibo_login/step4_sent.png' });
console.log('✅ 发送完成!');
// 检查是否成功
const currentUrl = page.url();
if (currentUrl.includes('weibo.com')) {
console.log('🎉 微博发布流程执行完毕!');
}
} else {
console.log('⚠️ 未找到发送按钮');
}
} else {
console.log('❌ 未找到发布框');
}
} catch (error) {
console.error('❌ 错误:', error.message);
} finally {
await browser.close();
}
}
postWeibo().catch(console.error);
@@ -0,0 +1,74 @@
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const delay = (ms) => new Promise(r => setTimeout(r, ms));
const TEST_CONTENT = '🤖 二维码登录测试成功!时间:' + new Date().toLocaleString('zh-CN');
(async () => {
const browser = await puppeteer.launch(getLaunchOptions());
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 访问主页
await page.goto('https://weibo.com', { waitUntil: 'networkidle2' });
await delay(3000);
console.log('🔗 URL:', page.url());
console.log('📄 标题:', await page.title());
// 找写微博按钮并点击
const writeBtn = await page.evaluateHandle(() => {
const btns = Array.from(document.querySelectorAll('button, a, div[role="button"]'));
return btns.find(b => b.textContent && b.textContent.includes('写微博'));
});
if (writeBtn) {
console.log('🖱️ 点击写微博...');
await writeBtn.click();
await delay(2000);
// 找发布框
const postBox = await page.$('textarea[placeholder*="有什么新鲜事"]');
if (postBox) {
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT);
await delay(1000);
// 找发送按钮
const sendBtn = await page.evaluateHandle(() => {
const btns = Array.from(document.querySelectorAll('button, a, div[role="button"]'));
return btns.find(b => b.textContent && b.textContent.includes('发送'));
});
if (sendBtn) {
console.log('🚀 点击发送...');
await sendBtn.click();
await delay(5000);
console.log('✅ 已发送!');
await page.screenshot({ path: '/tmp/weibo_qr_login/final_result.png' });
fs.copyFileSync('/tmp/weibo_qr_login/final_result.png', '/home/ubuntu/nanocode/audiobook-player/static/final_result.png');
// 验证
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
await delay(3000);
await page.screenshot({ path: '/tmp/weibo_qr_login/verify_post.png' });
fs.copyFileSync('/tmp/weibo_qr_login/verify_post.png', '/home/ubuntu/nanocode/audiobook-player/static/verify_post.png');
console.log('📸 验证截图已保存');
console.log('🎉 完成!');
} else {
console.log('⚠️ 未找到发送按钮');
}
} else {
console.log('⚠️ 未找到发布框');
}
} else {
console.log('⚠️ 未找到写微博按钮');
}
await browser.close();
})();
@@ -0,0 +1,75 @@
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const delay = (ms) => new Promise(r => setTimeout(r, ms));
const TEST_CONTENT = '🤖 二维码登录测试成功!时间:' + new Date().toLocaleString('zh-CN') + ' #测试';
(async () => {
const browser = await puppeteer.launch(getLaunchOptions());
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 访问主页
await page.goto('https://weibo.com', { waitUntil: 'networkidle2' });
await delay(3000);
console.log('🔗 URL:', page.url());
console.log('📄 标题:', await page.title());
// 在页面内执行所有操作
const result = await page.evaluate(async (content) => {
// 找写微博按钮
const writeBtn = Array.from(document.querySelectorAll('button, a, div[role="button"]'))
.find(b => b.textContent && b.textContent.includes('写微博'));
if (!writeBtn) return { error: '未找到写微博按钮' };
// 点击
writeBtn.click();
await new Promise(r => setTimeout(r, 2000));
// 找发布框
const postBox = document.querySelector('textarea[placeholder*="有什么新鲜事"]') ||
document.querySelector('textarea');
if (!postBox) return { error: '未找到发布框' };
// 输入内容
postBox.click();
postBox.value = content;
postBox.dispatchEvent(new Event('input', { bubbles: true }));
await new Promise(r => setTimeout(r, 1000));
// 找发送按钮
const sendBtn = Array.from(document.querySelectorAll('button, a, div[role="button"]'))
.find(b => b.textContent && b.textContent.includes('发送') && !b.disabled);
if (!sendBtn) return { error: '未找到发送按钮' };
// 点击发送
sendBtn.click();
return { success: true, message: '已点击发送' };
}, TEST_CONTENT);
console.log('📝 执行结果:', result);
if (result.success) {
await delay(5000);
await page.screenshot({ path: '/tmp/weibo_qr_login/after_send_js.png' });
fs.copyFileSync('/tmp/weibo_qr_login/after_send_js.png', '/home/ubuntu/nanocode/audiobook-player/static/after_send_js.png');
// 验证
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
await delay(3000);
await page.screenshot({ path: '/tmp/weibo_qr_login/final_verify.png' });
fs.copyFileSync('/tmp/weibo_qr_login/final_verify.png', '/home/ubuntu/nanocode/audiobook-player/static/final_verify.png');
console.log('✅ 流程完成!');
console.log('📸 截图: http://129.211.173.87:8899/screenshots/final_verify.png');
}
await browser.close();
})();
+204
View File
@@ -0,0 +1,204 @@
/**
* 微博登录助手 - 步骤1: 获取验证码
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const PHONE = process.argv[2] || '18688929976';
const SCREENSHOT_DIR = '/tmp/weibo_login';
// 延迟函数
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// 确保截图目录存在
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function weiboLoginStep1() {
console.log('🌐 正在打开微博登录页面...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 打开微博
await page.goto('https://weibo.com', {
waitUntil: 'networkidle2',
timeout: 60000,
});
console.log('✅ 页面加载完成');
await delay(3000);
// 保存初始页面截图
await page.screenshot({ path: `${SCREENSHOT_DIR}/01_initial.png` });
console.log('📸 初始页面已截图');
// 检查是否有登录框,如果没有可能需要点击登录按钮
const loginBtn = await page.$('a[href*="login"], .login, [node-type="loginBtn"], .W_btn_g');
if (loginBtn) {
const text = await page.evaluate(el => el.textContent, loginBtn);
console.log(`🔘 发现登录按钮: "${text?.trim()}",点击中...`);
await loginBtn.click();
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/02_login_modal.png` });
}
// 查找手机号输入框
console.log('📱 查找手机号输入框...');
const phoneSelectors = [
'input[name="username"]',
'input[name="phone"]',
'input[placeholder*="手机号"]',
'input[placeholder*="邮箱"]',
'input[type="tel"]',
'input[type="text"]',
'#loginname',
'.W_input',
'input[node-type="username"]',
'input[action-type="text_copy"]',
];
let phoneInput = null;
let usedSelector = '';
for (const sel of phoneSelectors) {
const inputs = await page.$$(sel);
for (const input of inputs) {
const type = await page.evaluate(el => el.type, input);
const placeholder = await page.evaluate(el => el.placeholder, input);
console.log(` 检查: ${sel} (type=${type}, placeholder=${placeholder})`);
if (!phoneInput) {
phoneInput = input;
usedSelector = sel;
}
}
if (phoneInput) break;
}
if (!phoneInput) {
console.log('⚠️ 尝试获取所有输入框...');
const allInputs = await page.$$('input');
console.log(` 找到 ${allInputs.length} 个输入框`);
if (allInputs.length > 0) {
phoneInput = allInputs[0];
}
}
if (!phoneInput) {
throw new Error('未找到输入框');
}
console.log(`✅ 找到输入框: ${usedSelector}`);
// 清空并输入手机号
await phoneInput.click({ clickCount: 3 }); // 全选
await phoneInput.type(PHONE, { delay: 50 });
console.log(`✅ 手机号已输入: ${PHONE}`);
await page.screenshot({ path: `${SCREENSHOT_DIR}/03_phone_entered.png` });
// 查找"获取验证码"按钮
console.log('🔍 查找"获取验证码"按钮...');
const codeBtnSelectors = [
'a[action-type="get_code"]',
'a[node-type="btn_code"]',
'.W_btn_g',
'.get_code',
'a:has-text("获取")',
'a:has-text("验证码")',
'button:has-text("获取")',
'button:has-text("验证码")',
];
let codeBtn = null;
for (const sel of codeBtnSelectors) {
try {
codeBtn = await page.$(sel);
if (codeBtn) {
const text = await page.evaluate(el => el.textContent, codeBtn);
console.log(` 找到按钮: "${text?.trim()}" (${sel})`);
break;
}
} catch (e) {}
}
// 如果没找到,尝试查找所有按钮/链接
if (!codeBtn) {
console.log(' 尝试查找所有按钮...');
const allBtns = await page.$$('a, button');
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text && (text.includes('获取') || text.includes('验证码') || text.includes('code'))) {
console.log(` 候选: "${text.trim()}"`);
if (!codeBtn && text.includes('获取')) {
codeBtn = btn;
}
}
}
}
if (codeBtn) {
const btnText = await page.evaluate(el => el.textContent, codeBtn);
console.log(`\n🖱️ 点击按钮: "${btnText?.trim()}"`);
await codeBtn.click();
console.log('✅ 已点击"获取验证码"');
// 等待几秒钟,让短信发送
await delay(5000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/04_code_requested.png` });
// 检查是否有提示信息
const tips = await page.$$eval('.W_layer, .W_tips, .tip, .error, .info', els =>
els.map(el => el.textContent?.trim()).filter(Boolean)
);
if (tips.length > 0) {
console.log('\n💬 页面提示:', tips.slice(0, 3));
}
} else {
console.log('⚠️ 未找到验证码按钮,请查看截图手动确认');
}
// 保存当前页面HTML用于分析
const html = await page.content();
fs.writeFileSync(`${SCREENSHOT_DIR}/page.html`, html);
console.log('\n' + '='.repeat(60));
console.log('📋 步骤1完成,当前状态:');
console.log(` 手机号: ${PHONE}`);
console.log(` 验证码: ⏳ 等待发送...`);
console.log('\n📸 截图文件:');
fs.readdirSync(SCREENSHOT_DIR).forEach(f => {
console.log(` - ${SCREENSHOT_DIR}/${f}`);
});
console.log('\n📱 请检查手机,收到验证码后告诉我');
console.log('格式: 验证码 123456');
console.log('='.repeat(60));
// 保存浏览器状态,等待下一步
await page.screenshot({ path: `${SCREENSHOT_DIR}/05_waiting_code.png` });
// 保持浏览器实例供后续使用(通过文件记录WSEndpoint)
const wsEndpoint = browser.wsEndpoint();
fs.writeFileSync(`${SCREENSHOT_DIR}/browser_ws.txt`, wsEndpoint);
console.log('\n⏳ 浏览器保持连接,等待下一步...');
// 保持运行
await new Promise(() => {});
} catch (error) {
console.error('❌ 操作失败:', error.message);
await browser.close();
process.exit(1);
}
}
weiboLoginStep1().catch(console.error);
@@ -0,0 +1,133 @@
/**
* 微博登录助手 V2 - 直接访问登录页面
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const PHONE = process.argv[2] || '18688929976';
const SCREENSHOT_DIR = '/tmp/weibo_login';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function weiboLogin() {
console.log('🌐 正在打开微博登录页面...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 直接访问登录页面
await page.goto('https://weibo.com/newlogin?tabtype=weibo', {
waitUntil: 'networkidle2',
timeout: 60000,
});
console.log('✅ 登录页面加载完成');
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/login_page.png` });
console.log('📸 已截图');
// 查找并点击短信登录选项卡
const tabs = await page.$$('div[role="tab"], .tab, [class*="tab"]');
console.log(`找到 ${tabs.length} 个选项卡`);
for (const tab of tabs) {
const text = await page.evaluate(el => el.textContent, tab);
console.log(` 选项卡: "${text?.trim()}"`);
if (text && text.includes('短信')) {
console.log('🖱️ 点击短信登录选项卡');
await tab.click();
await delay(2000);
break;
}
}
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_tab.png` });
// 查找手机号输入框
console.log('📱 查找手机号输入框...');
const phoneInput = await page.$('input[type="tel"], input[placeholder*="手机号"], input[name="phone"]');
if (phoneInput) {
await phoneInput.type(PHONE, { delay: 50 });
console.log(`✅ 手机号已输入: ${PHONE}`);
} else {
// 尝试所有输入框
const inputs = await page.$$('input');
console.log(`尝试 ${inputs.length} 个输入框...`);
for (const input of inputs) {
const placeholder = await page.evaluate(el => el.placeholder, input);
const type = await page.evaluate(el => el.type, input);
console.log(` input type=${type} placeholder="${placeholder}"`);
if (type === 'tel' || (placeholder && placeholder.includes('手机'))) {
await input.type(PHONE, { delay: 50 });
console.log(`✅ 手机号已输入: ${PHONE}`);
break;
}
}
}
await page.screenshot({ path: `${SCREENSHOT_DIR}/phone_entered.png` });
// 查找获取验证码按钮
console.log('🔍 查找"获取验证码"按钮...');
const buttons = await page.$$('button, a, div[role="button"]');
let codeBtn = null;
for (const btn of buttons) {
const text = await page.evaluate(el => el.textContent, btn);
if (text && (text.includes('获取') || text.includes('验证码') || text.includes('code'))) {
console.log(` 找到按钮: "${text.trim()}"`);
codeBtn = btn;
break;
}
}
if (codeBtn) {
console.log('🖱️ 点击获取验证码按钮...');
await codeBtn.click();
console.log('✅ 已发送验证码请求');
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/code_requested.png` });
} else {
console.log('⚠️ 未找到验证码按钮');
}
console.log('\n' + '='.repeat(60));
console.log('📋 步骤完成,当前状态:');
console.log(` 手机号: ${PHONE}`);
console.log(` 验证码: ⏳ 等待发送...`);
console.log('\n📸 截图文件:');
fs.readdirSync(SCREENSHOT_DIR).forEach(f => {
console.log(` - ${SCREENSHOT_DIR}/${f}`);
});
console.log('\n📱 请检查手机 18688929976');
console.log('收到验证码后告诉我,格式: 验证码 123456');
console.log('='.repeat(60));
// 保存浏览器状态供后续使用
const cookies = await page.cookies();
fs.writeFileSync(`${SCREENSHOT_DIR}/cookies_step1.json`, JSON.stringify(cookies, null, 2));
// 保持运行等待验证码
await new Promise(() => {});
} catch (error) {
console.error('❌ 错误:', error.message);
await browser.close();
process.exit(1);
}
}
weiboLogin().catch(console.error);
@@ -0,0 +1,140 @@
/**
* 扫码登录后发送测试微博
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const SCREENSHOT_DIR = '/tmp/weibo_qr_login';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function postAfterQR() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
try {
// 访问微博主页验证登录
console.log('🔄 访问微博主页验证登录...');
await page.goto('https://weibo.com', { waitUntil: 'networkidle2', timeout: 60000 });
await delay(3000);
const currentUrl = page.url();
const title = await page.title();
console.log(`🔗 URL: ${currentUrl}`);
console.log(`📄 标题: ${title}`);
await page.screenshot({ path: `${SCREENSHOT_DIR}/logged_in_home.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/logged_in_home.png`, `/home/ubuntu/nanocode/audiobook-player/static/logged_in_home.png`);
// 检查是否已登录(看有没有"写微博"按钮)
const allBtns = await page.$$('button, a');
let hasWriteBtn = false;
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('写微博')) {
hasWriteBtn = true;
break;
}
}
if (hasWriteBtn) {
console.log('✅ 检测到"写微博"按钮,登录成功!');
} else {
console.log('⚠️ 未检测到"写微博"按钮,可能还在登录页');
}
// 尝试发送测试微博
console.log('\n📝 准备发送测试微博...');
const TEST_CONTENT = '🤖 二维码登录测试成功!时间:' + new Date().toLocaleString('zh-CN');
// 点击"写微博"
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('写微博')) {
console.log('🖱️ 点击"写微博"...');
await btn.click();
break;
}
}
await delay(2000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/compose_opened.png` });
// 查找发布框
let postBox = await page.$('textarea[placeholder*="有什么新鲜事"]');
if (!postBox) {
const textareas = await page.$$('textarea');
if (textareas.length > 0) postBox = textareas[0];
}
if (postBox) {
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT);
await delay(1000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/content_typed.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/content_typed.png`, `/home/ubuntu/nanocode/audiobook-player/static/content_typed.png`);
// 查找发送按钮
let sendBtn = null;
const allBtns2 = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns2) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('发送')) {
sendBtn = btn;
console.log(`🖱️ 找到发送按钮: "${text.trim()}"`);
break;
}
}
if (sendBtn) {
// 检查按钮是否可用
const isEnabled = await page.evaluate(el => !el.disabled, sendBtn);
console.log(`📊 按钮状态: ${isEnabled ? '可用' : '禁用'}`);
console.log('🚀 点击发送...');
await sendBtn.click();
await delay(5000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_send.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/after_send.png`, `/home/ubuntu/nanocode/audiobook-player/static/after_send.png`);
console.log('✅ 发送按钮已点击!');
// 验证是否发送成功
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/verify.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/verify.png`, `/home/ubuntu/nanocode/audiobook-player/static/verify.png`);
console.log('📸 已截图验证页面');
console.log('\n🎉 测试微博发送流程已完成!');
} else {
console.log('⚠️ 未找到发送按钮');
}
} else {
console.log('⚠️ 未找到发布框');
}
} catch (error) {
console.error('❌ 错误:', error.message);
await page.screenshot({ path: `${SCREENSHOT_DIR}/error_post.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/error_post.png`, `/home/ubuntu/nanocode/audiobook-player/static/error_post.png`);
} finally {
await browser.close();
}
}
postAfterQR().catch(console.error);
@@ -0,0 +1,199 @@
/**
* 使用 Cookie 登录微博 V2 - 直接访问发布页面
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const COOKIE_FILE = '/home/ubuntu/weibo_cookies.txt';
const TEST_CONTENT = '这是一条来自自动化脚本的测试微博 🤖 #测试 #自动化 ' + new Date().toLocaleString();
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function postWeibo() {
console.log('🌐 正在启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 读取 Cookie
console.log('🍪 正在加载 Cookie...');
const cookieContent = fs.readFileSync(COOKIE_FILE, 'utf-8');
const cookies = JSON.parse(cookieContent);
console.log(`📋 加载了 ${cookies.length} 个 Cookie`);
// 先访问微博并设置 cookie
await page.goto('https://weibo.com', { waitUntil: 'domcontentloaded', timeout: 60000 });
// 设置 Cookie
for (const cookie of cookies) {
try {
await page.setCookie({
name: cookie.name,
value: cookie.value,
domain: cookie.domain,
path: cookie.path || '/',
secure: cookie.secure || false,
httpOnly: cookie.httpOnly || false,
...(cookie.expirationDate && { expires: Math.floor(cookie.expirationDate) })
});
} catch (e) {}
}
console.log('✅ Cookie 设置完成');
// 访问个人主页验证登录
console.log('🔄 访问个人主页验证登录...');
await page.goto('https://weibo.com/u/me', { waitUntil: 'networkidle2', timeout: 60000 });
await delay(3000);
await page.screenshot({ path: '/tmp/weibo_login/profile_page.png' });
console.log('📸 已截图个人主页');
// 获取页面标题和URL
const title = await page.title();
const url = page.url();
console.log(`📄 页面标题: ${title}`);
console.log(`🔗 当前URL: ${url}`);
// 检查是否在登录页面
if (url.includes('login') || title.includes('登录')) {
console.log('❌ Cookie 已过期,需要重新登录');
return;
}
// 尝试打开发布器
console.log('\n📝 尝试打开发布器...');
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2', timeout: 60000 });
await delay(3000);
// 查找发布框 - 尝试多种选择器
const selectors = [
'textarea[title="微博内容"]',
'.W_input',
'[node-type="textEl"]',
'textarea[placeholder*="有什么新鲜事"]',
'textarea[placeholder*="想说什么"]',
'[class*="publish"] textarea',
'[class*="post"] textarea',
];
let postBox = null;
for (const sel of selectors) {
postBox = await page.$(sel);
if (postBox) {
console.log(`✅ 找到发布框: ${sel}`);
break;
}
}
// 如果没找到,尝试点击"发布"按钮
if (!postBox) {
console.log('🖱️ 尝试点击发布按钮...');
// 通过文本内容查找按钮
const allLinks = await page.$$('a, button, div[role="button"]');
for (const link of allLinks) {
const text = await page.evaluate(el => el.textContent, link);
if (text && (text.includes('发布') || text.includes('发微博'))) {
console.log(` 找到发布按钮: "${text.trim()}"`);
await link.click();
await delay(2000);
break;
}
}
// 再次查找发布框
for (const sel of selectors) {
postBox = await page.$(sel);
if (postBox) break;
}
}
if (postBox) {
console.log('✅ 准备发送微博...');
// 聚焦并输入
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT);
await delay(1000);
await page.screenshot({ path: '/tmp/weibo_login/before_send.png' });
// 查找发送按钮
let sendBtn = null;
// 先尝试标准选择器
const sendSelectors = [
'a[title="发布"]',
'[action-type="submit"]',
'[node-type="submit"]',
'.W_btn_a',
'button[type="submit"]',
];
for (const sel of sendSelectors) {
sendBtn = await page.$(sel);
if (sendBtn) {
const text = await page.evaluate(el => el.textContent, sendBtn);
console.log(` 找到发送按钮: "${text?.trim()}" (${sel})`);
break;
}
}
// 如果没找到,通过文本搜索
if (!sendBtn) {
const allBtns = await page.$$('a, button, div[role="button"]');
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text && (text.includes('发送') || text.includes('发布'))) {
console.log(` 通过文本找到按钮: "${text.trim()}"`);
sendBtn = btn;
break;
}
}
}
if (sendBtn) {
await sendBtn.click();
console.log('🚀 已点击发送!');
await delay(5000);
await page.screenshot({ path: '/tmp/weibo_login/after_send.png' });
console.log('📸 已截图保存结果');
// 检查是否发送成功
const success = await page.$eval('.success, .W_tips, [class*="success"]', el => el.textContent).catch(() => null);
if (success) {
console.log('✅ 发送结果:', success);
}
console.log('\n🎉 测试微博发送流程完成!');
} else {
console.log('⚠️ 未找到发送按钮,请查看 /tmp/weibo_login/before_send.png');
}
} else {
console.log('⚠️ 未找到发布框');
await page.screenshot({ path: '/tmp/weibo_login/no_postbox.png' });
}
} catch (error) {
console.error('❌ 错误:', error.message);
try {
await page.screenshot({ path: '/tmp/weibo_login/error.png' });
} catch (e) {}
} finally {
await browser.close();
}
}
postWeibo().catch(console.error);
@@ -0,0 +1,143 @@
/**
* 使用 Cookie 登录微博并发送测试微博
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const COOKIE_FILE = '/home/ubuntu/weibo_cookies.txt';
const TEST_CONTENT = '这是一条来自自动化脚本的测试微博 🤖 #测试 #自动化';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function postWeibo() {
console.log('🌐 正在启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 读取并设置 Cookie
console.log('🍪 正在加载 Cookie...');
const cookieContent = fs.readFileSync(COOKIE_FILE, 'utf-8');
let cookies;
try {
cookies = JSON.parse(cookieContent);
// 如果是数组格式,直接使用
if (!Array.isArray(cookies)) {
// 如果是对象格式,转换为数组
cookies = Object.entries(cookies).map(([name, value]) => ({
name,
value,
domain: '.weibo.com',
path: '/'
}));
}
} catch (e) {
// 尝试解析为 name=value; 格式
cookies = cookieContent.split(';').map(pair => {
const [name, value] = pair.trim().split('=');
return { name, value, domain: '.weibo.com', path: '/' };
}).filter(c => c.name && c.value);
}
console.log(`📋 加载了 ${cookies.length} 个 Cookie`);
// 先访问微博主页,再设置 cookie
await page.goto('https://weibo.com', { waitUntil: 'networkidle2', timeout: 60000 });
await delay(2000);
// 设置 Cookie
for (const cookie of cookies) {
try {
// 确保必要的字段
const cookieToSet = {
name: cookie.name,
value: cookie.value,
domain: cookie.domain || '.weibo.com',
path: cookie.path || '/',
secure: cookie.secure || true,
httpOnly: cookie.httpOnly || false,
};
if (cookie.expirationDate) {
cookieToSet.expires = Math.floor(cookie.expirationDate);
}
await page.setCookie(cookieToSet);
} catch (e) {
// 忽略单个 cookie 设置错误
}
}
console.log('✅ Cookie 设置完成');
// 刷新页面,验证登录状态
console.log('🔄 刷新页面验证登录...');
await page.reload({ waitUntil: 'networkidle2', timeout: 60000 });
await delay(3000);
// 截图查看当前状态
await page.screenshot({ path: '/tmp/weibo_login/cookie_login_status.png' });
console.log('📸 已截图保存登录状态');
// 检查是否已登录(查找用户名或发布框)
const userName = await page.$eval('.user-name, .name, [class*="user"]', el => el.textContent).catch(() => null);
const postBox = await page.$('[class*="publish"], [class*="post"], textarea[class*="textarea"]').catch(() => null);
if (userName) {
console.log(`✅ 检测到用户名: ${userName.trim()}`);
}
if (postBox) {
console.log('✅ 找到发布框,准备发送微博...');
// 点击发布框
await postBox.click();
await delay(500);
// 输入内容
await postBox.type(TEST_CONTENT, { delay: 50 });
console.log('✍️ 已输入内容:', TEST_CONTENT);
// 查找发送按钮
const sendBtn = await page.$('[class*="send"], [class*="submit"], button:has-text("发送"), a:has-text("发送")').catch(() => null);
if (sendBtn) {
console.log('🖱️ 点击发送按钮...');
await sendBtn.click();
await delay(3000);
await page.screenshot({ path: '/tmp/weibo_login/after_post.png' });
console.log('📸 已截图保存发送结果');
console.log('\n✅ 测试微博发送完成!');
} else {
console.log('⚠️ 未找到发送按钮');
}
} else {
console.log('⚠️ 未找到发布框,可能未登录成功或页面结构不同');
// 尝试查找登录按钮
const loginBtn = await page.$('a:has-text("登录"), .login, [class*="login"]').catch(() => null);
if (loginBtn) {
console.log('❌ 检测到登录按钮,Cookie 可能已过期');
}
}
} catch (error) {
console.error('❌ 操作失败:', error.message);
await page.screenshot({ path: '/tmp/weibo_login/error.png' });
} finally {
await browser.close();
}
}
postWeibo().catch(console.error);
@@ -0,0 +1,183 @@
/**
* 微博二维码登录测试
* 1. 打开登录页,截取二维码
* 2. 等待用户扫码
* 3. 验证登录并发微博
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const SCREENSHOT_DIR = '/tmp/weibo_qr_login';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// 确保目录存在
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function weiboQRLogin() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 访问微博登录页
console.log('🔄 访问微博登录页...');
await page.goto('https://weibo.com/newlogin?tabtype=weibo', {
waitUntil: 'networkidle2',
timeout: 60000,
});
await delay(3000);
console.log('✅ 登录页加载完成');
// 截图 - 应该包含二维码
const qrScreenshot = `${SCREENSHOT_DIR}/qr_code.png`;
await page.screenshot({ path: qrScreenshot });
console.log(`📸 已截图: ${qrScreenshot}`);
// 检查页面是否有二维码元素
const qrSelectors = [
'img[src*="qr"]',
'[class*="qr"]',
'[class*="qrcode"]',
'canvas',
];
let qrFound = false;
for (const sel of qrSelectors) {
const el = await page.$(sel);
if (el) {
console.log(`✅ 找到二维码元素: ${sel}`);
qrFound = true;
break;
}
}
if (!qrFound) {
console.log('⚠️ 未明显找到二维码元素,但已截图整页');
}
console.log('\n' + '='.repeat(60));
console.log('📧 请查收邮件中的二维码截图');
console.log('📱 请使用微博 App 扫描二维码');
console.log('⏳ 等待 60 秒让您扫码...');
console.log('='.repeat(60) + '\n');
// 等待用户扫码
await delay(60000);
// 检查登录状态
console.log('🔍 检查登录状态...');
await page.reload({ waitUntil: 'networkidle2' });
await delay(3000);
const currentUrl = page.url();
const title = await page.title();
console.log(`🔗 当前URL: ${currentUrl}`);
console.log(`📄 页面标题: ${title}`);
// 截图当前状态
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_scan.png` });
// 判断是否登录成功
const isLoggedIn = !currentUrl.includes('login') && !title.includes('登录');
if (isLoggedIn) {
console.log('✅ 登录成功!');
// 尝试发微博
console.log('\n📝 尝试发送测试微博...');
await postTestWeibo(page);
} else {
console.log('⏳ 可能还未扫码或扫码未完成');
console.log('💡 如果需要更多时间,请告诉我延长等待');
}
} catch (error) {
console.error('❌ 错误:', error.message);
await page.screenshot({ path: `${SCREENSHOT_DIR}/error.png` });
} finally {
await browser.close();
}
}
async function postTestWeibo(page) {
const TEST_CONTENT = '🤖 二维码登录测试 ' + new Date().toLocaleString('zh-CN');
try {
// 点击"写微博"
const allBtns = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('写微博')) {
await btn.click();
console.log('🖱️ 点击"写微博"');
break;
}
}
await delay(2000);
// 查找发布框
let postBox = await page.$('textarea[placeholder*="有什么新鲜事"]');
if (!postBox) {
const textareas = await page.$$('textarea');
if (textareas.length > 0) postBox = textareas[0];
}
if (postBox) {
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT);
await delay(1000);
// 查找发送按钮
let sendBtn = null;
const allBtns2 = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns2) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('发送')) {
sendBtn = btn;
break;
}
}
if (sendBtn) {
console.log('🚀 点击发送...');
await sendBtn.click();
await delay(5000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/post_result.png` });
console.log('✅ 发送完成!');
// 验证
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
await delay(3000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/verify_post.png` });
console.log('📸 已截图验证页面');
} else {
console.log('⚠️ 未找到发送按钮');
}
} else {
console.log('⚠️ 未找到发布框');
}
} catch (error) {
console.error('❌ 发微博失败:', error.message);
}
}
weiboQRLogin().catch(console.error);
@@ -0,0 +1,210 @@
/**
* 微博二维码登录测试 V2
* 1. 访问微博首页
* 2. 点击"登录/注册"按钮
* 3. 截取二维码
* 4. 等待扫码并验证
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const SCREENSHOT_DIR = '/tmp/weibo_qr_login';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function weiboQRLogin() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 访问微博首页
console.log('🔄 访问微博首页...');
await page.goto('https://weibo.com', {
waitUntil: 'networkidle2',
timeout: 60000,
});
await delay(3000);
console.log('✅ 首页加载完成');
// 截图 - 首页状态
await page.screenshot({ path: `${SCREENSHOT_DIR}/homepage.png` });
// 查找并点击"登录"按钮
console.log('🔍 查找登录按钮...');
let loginBtn = null;
// 通过文本查找
const allLinks = await page.$$('a, button, div[role="button"]');
for (const link of allLinks) {
const text = await page.evaluate(el => el.textContent, link);
if (text && (text.trim() === '登录' || text.includes('登录/注册'))) {
console.log(`✅ 找到登录按钮: "${text.trim()}"`);
loginBtn = link;
break;
}
}
if (!loginBtn) {
console.log('⚠️ 通过文本未找到,尝试选择器...');
loginBtn = await page.$('a[href*="login"], .login, [node-type="loginBtn"]');
}
if (loginBtn) {
console.log('🖱️ 点击登录按钮...');
try {
await loginBtn.click();
} catch (e) {
// 如果点击失败,尝试 JavaScript 点击
console.log(' 普通点击失败,尝试 JS 点击...');
await page.evaluate(el => el.click(), loginBtn);
}
await delay(3000);
console.log('✅ 已点击登录');
} else {
console.log('❌ 未找到登录按钮,尝试直接访问登录链接');
await page.goto('https://weibo.com/newlogin', { waitUntil: 'networkidle2' });
await delay(3000);
}
// 截图 - 应该显示登录弹窗/页面
await page.screenshot({ path: `${SCREENSHOT_DIR}/login_page.png` });
console.log('📸 已截图登录页面');
// 检查是否有二维码
const qrSelectors = [
'img[src*="qr"]',
'[class*="qr"]',
'[class*="qrcode"]',
'canvas',
];
let qrFound = false;
for (const sel of qrSelectors) {
const el = await page.$(sel);
if (el) {
console.log(`✅ 找到二维码元素: ${sel}`);
qrFound = true;
break;
}
}
// 专门截图二维码区域
const qrScreenshot = `${SCREENSHOT_DIR}/qr_code_v2.png`;
await page.screenshot({ path: qrScreenshot });
console.log(`📸 已保存: ${qrScreenshot}`);
console.log('\n' + '='.repeat(60));
console.log('📧 请查看登录页面截图');
console.log('📱 请使用微博 App 扫描二维码');
console.log('⏳ 等待扫码...');
console.log('='.repeat(60) + '\n');
// 等待用户扫码
await delay(60000);
// 检查登录状态
console.log('🔍 检查登录状态...');
await page.reload({ waitUntil: 'networkidle2' });
await delay(3000);
const currentUrl = page.url();
const title = await page.title();
console.log(`🔗 当前URL: ${currentUrl}`);
console.log(`📄 页面标题: ${title}`);
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_scan.png` });
// 判断是否登录成功
const isLoggedIn = !currentUrl.includes('login') && !title.includes('登录');
if (isLoggedIn) {
console.log('✅ 登录成功!');
// 尝试发微博
await postTestWeibo(page);
} else {
console.log('⏳ 可能还未扫码完成');
}
} catch (error) {
console.error('❌ 错误:', error.message);
try {
if (typeof page !== 'undefined') {
await page.screenshot({ path: `${SCREENSHOT_DIR}/error.png` });
}
} catch(e) {}
} finally {
await browser.close();
}
}
async function postTestWeibo(page) {
const TEST_CONTENT = '🤖 二维码登录测试 ' + new Date().toLocaleString('zh-CN');
try {
// 点击"写微博"
const allBtns = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('写微博')) {
await btn.click();
console.log('🖱️ 点击"写微博"');
break;
}
}
await delay(2000);
// 查找发布框
let postBox = await page.$('textarea[placeholder*="有什么新鲜事"]');
if (!postBox) {
const textareas = await page.$$('textarea');
if (textareas.length > 0) postBox = textareas[0];
}
if (postBox) {
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT);
await delay(1000);
// 查找发送按钮
let sendBtn = null;
const allBtns2 = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns2) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('发送')) {
sendBtn = btn;
break;
}
}
if (sendBtn) {
console.log('🚀 点击发送...');
await sendBtn.click();
await delay(5000);
await page.screenshot({ path: `${SCREENSHOT_DIR}/post_result.png` });
console.log('✅ 发送完成!');
}
}
} catch (error) {
console.error('❌ 发微博失败:', error.message);
}
}
weiboQRLogin().catch(console.error);
+120
View File
@@ -0,0 +1,120 @@
/**
* 微博二维码登录 V3 - 直接访问登录地址
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const SCREENSHOT_DIR = '/tmp/weibo_qr_login';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function weiboQRLogin() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
try {
// 直接访问登录地址
const loginUrl = 'https://passport.weibo.com/sso/signin?entry=miniblog&source=miniblog&disp=popup&url=https%3A%2F%2Fweibo.com%2Fnewlogin%3Ftabtype%3Dweibo%26gid%3D102803%26openLoginLayer%3D0%26url%3Dhttps%3A%2F%2Fweibo.com%2F&from=weibopro';
console.log('🔄 访问登录页面...');
await page.goto(loginUrl, {
waitUntil: 'networkidle2',
timeout: 60000,
});
await delay(5000);
console.log('✅ 登录页面加载完成');
// 截图
await page.screenshot({ path: `${SCREENSHOT_DIR}/passport_login.png` });
console.log('📸 已截图登录页面');
// 检查页面内容
const title = await page.title();
console.log(`📄 页面标题: ${title}`);
console.log(`🔗 当前URL: ${page.url()}`);
// 查找二维码
const qrSelectors = [
'img[src*="qr"]',
'[class*="qr"]',
'[class*="qrcode"]',
'canvas',
'[class*="code"]',
];
let qrFound = false;
for (const sel of qrSelectors) {
const el = await page.$(sel);
if (el) {
console.log(`✅ 找到二维码元素: ${sel}`);
qrFound = true;
// 专门截图二维码元素
try {
await el.screenshot({ path: `${SCREENSHOT_DIR}/qr_only.png` });
console.log('📸 已单独截图二维码');
} catch(e) {}
break;
}
}
if (!qrFound) {
console.log('⚠️ 未明显找到二维码元素');
}
// 复制到 web 目录
const fs = require('fs');
const files = ['passport_login.png', 'qr_only.png'].filter(f => fs.existsSync(`${SCREENSHOT_DIR}/${f}`));
for (const file of files) {
fs.copyFileSync(`${SCREENSHOT_DIR}/${file}`, `/home/ubuntu/nanocode/audiobook-player/static/${file}`);
console.log(`📤 已上传: http://129.211.173.87:8899/screenshots/${file}`);
}
console.log('\n' + '='.repeat(60));
console.log('📱 请使用微博 App 扫描二维码');
console.log('⏳ 等待 60 秒...');
console.log('='.repeat(60) + '\n');
// 等待扫码
await delay(60000);
// 检查状态
console.log('🔍 检查登录状态...');
await page.reload({ waitUntil: 'networkidle2' });
await delay(3000);
console.log(`🔗 当前URL: ${page.url()}`);
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_scan.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/after_scan.png`, `/home/ubuntu/nanocode/audiobook-player/static/after_scan.png`);
// 判断是否跳转成功
if (page.url().includes('weibo.com') && !page.url().includes('passport')) {
console.log('✅ 看起来登录成功了!');
} else {
console.log('⏳ 扫码可能未完成');
}
} catch (error) {
console.error('❌ 错误:', error.message);
await page.screenshot({ path: `${SCREENSHOT_DIR}/error.png` });
fs.copyFileSync(`${SCREENSHOT_DIR}/error.png`, `/home/ubuntu/nanocode/audiobook-player/static/error.png`);
} finally {
await browser.close();
}
}
weiboQRLogin().catch(console.error);
@@ -0,0 +1,177 @@
/**
* 微博发送 V3 - 带详细错误检查
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const COOKIE_FILE = '/home/ubuntu/weibo_cookies.txt';
const TEST_CONTENT = '🤖 自动化测试 ' + Date.now();
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
async function postWeibo() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
// 监听控制台日志
page.on('console', msg => {
if (msg.type() === 'error') {
console.log(' [页面错误]', msg.text());
}
});
// 加载 Cookie
console.log('🍪 加载 Cookie...');
const cookies = JSON.parse(fs.readFileSync(COOKIE_FILE, 'utf-8'));
await page.goto('https://weibo.com', { waitUntil: 'domcontentloaded' });
for (const c of cookies) {
try {
await page.setCookie({
name: c.name,
value: c.value,
domain: c.domain,
path: c.path || '/'
});
} catch (e) {}
}
// 访问主页
console.log('🔄 访问微博主页...');
await page.goto('https://weibo.com', { waitUntil: 'networkidle2' });
await delay(3000);
// 点击"写微博"
console.log('🖱️ 点击"写微博"...');
const allBtns = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('写微博')) {
await btn.click();
console.log(' 已点击');
break;
}
}
await delay(2000);
await page.screenshot({ path: '/tmp/weibo_login/v3_compose.png' });
// 查找发布框
console.log('📝 查找发布框...');
let postBox = await page.$('textarea[placeholder*="有什么新鲜事"]');
if (!postBox) {
const textareas = await page.$$('textarea');
console.log(` 找到 ${textareas.length} 个 textarea`);
if (textareas.length > 0) {
postBox = textareas[0];
}
}
if (!postBox) {
throw new Error('未找到发布框');
}
// 输入内容
await postBox.click();
await delay(500);
await postBox.type(TEST_CONTENT, { delay: 30 });
console.log('✍️ 已输入:', TEST_CONTENT);
await delay(1000);
await page.screenshot({ path: '/tmp/weibo_login/v3_typed.png' });
// 查找发送按钮
console.log('🔍 查找发送按钮...');
let sendBtn = null;
let sendText = '';
const allBtns2 = await page.$$('button, a, div[role="button"]');
for (const btn of allBtns2) {
const text = await page.evaluate(el => el.textContent, btn);
if (text?.includes('发送') || text?.includes('发布')) {
sendBtn = btn;
sendText = text.trim();
break;
}
}
if (!sendBtn) {
throw new Error('未找到发送按钮');
}
console.log(` 找到按钮: "${sendText}"`);
// 检查按钮是否可点击
const isEnabled = await page.evaluate(el => {
return !el.disabled && !el.getAttribute('disabled');
}, sendBtn);
console.log(` 按钮状态: ${isEnabled ? '可用' : '禁用'}`);
// 点击前截图
await page.screenshot({ path: '/tmp/weibo_login/v3_before_click.png' });
// 点击发送
console.log('🚀 点击发送...');
await sendBtn.click();
// 等待并检查页面变化
console.log('⏳ 等待响应...');
await delay(5000);
// 检查是否有错误提示
const errorMsg = await page.$eval('.error, .W_layer, .tips, [class*="error"]', el => el.textContent).catch(() => null);
if (errorMsg) {
console.log('⚠️ 页面提示:', errorMsg);
}
// 检查当前URL
const currentUrl = page.url();
console.log('🔗 当前URL:', currentUrl);
// 截图
await page.screenshot({ path: '/tmp/weibo_login/v3_after_send.png' });
console.log('📸 已截图');
// 检查最新微博
await page.goto('https://weibo.com/u/me?tabtype=weibo', { waitUntil: 'networkidle2' });
await delay(3000);
const posts = await page.evaluate(() => {
const items = Array.from(document.querySelectorAll('[class*="card"], [class*="item"]'));
return items.slice(0, 2).map(item => {
const text = item.textContent || '';
return text.substring(0, 100);
});
});
console.log('\n📱 最新微博:');
posts.forEach((p, i) => console.log(` [${i+1}] ${p.substring(0, 80)}...`));
if (posts.some(p => p.includes('自动化测试'))) {
console.log('\n✅ 发送成功!');
} else {
console.log('\n❌ 未找到刚发的微博,发送可能失败了');
}
} catch (error) {
console.error('❌ 错误:', error.message);
try {
await page.screenshot({ path: '/tmp/weibo_login/v3_error.png' });
} catch(e) {}
} finally {
await browser.close();
}
}
postWeibo().catch(console.error);
@@ -0,0 +1,130 @@
/**
* 微博登录 - 分步骤执行
* Step 1: 打开登录页,截图二维码
* Step 2: 等待用户扫码
* Step 3: 保存登录后的 Cookie
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../config/browser');
const fs = require('fs');
const SCREENSHOT_DIR = '/tmp/weibo_step_login';
const COOKIE_FILE = '/home/ubuntu/weibo_cookies_qr.json';
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// 确保目录存在
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
}
async function stepByStepLogin() {
console.log('🌐 启动浏览器...');
const browser = await puppeteer.launch({
...getLaunchOptions(),
headless: true,
});
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 900 });
try {
// ===== STEP 1: 打开登录页并截图 =====
const loginUrl = 'https://passport.weibo.com/sso/signin?entry=miniblog&source=miniblog&disp=popup&url=https%3A%2F%2Fweibo.com%2Fnewlogin%3Ftabtype%3Dweibo%26gid%3D102803%26openLoginLayer%3D0%26url%3Dhttps%3A%2F%2Fweibo.com%2F&from=weibopro';
console.log('\n📍 Step 1: 打开登录页面...');
await page.goto(loginUrl, {
waitUntil: 'networkidle2',
timeout: 60000,
});
await delay(5000);
console.log('✅ 登录页已加载');
// 截图二维码
const qrScreenshot = `${SCREENSHOT_DIR}/qr_step1.png`;
await page.screenshot({ path: qrScreenshot, fullPage: false });
console.log(`📸 已截图: ${qrScreenshot}`);
// 复制到 web 目录
fs.copyFileSync(qrScreenshot, `/home/ubuntu/nanocode/audiobook-player/static/qr_step1.png`);
console.log('📤 已上传: http://129.211.173.87:8899/screenshots/qr_step1.png');
console.log('\n' + '='.repeat(60));
console.log('🛑 请扫码!');
console.log('📱 打开微博 App → 扫一扫');
console.log('🔗 http://129.211.173.87:8899/screenshots/qr_step1.png');
console.log('⏳ 扫码完成后告诉我,我会继续保存 Cookie');
console.log('='.repeat(60) + '\n');
// 保存浏览器实例,等待用户指令
// 我们会保持浏览器打开,但需要等待用户回复
// 测试:等待一段时间检查是否已扫码
let loggedIn = false;
let attempts = 0;
const maxAttempts = 60; // 最多等待60次,每次2秒 = 120秒
while (!loggedIn && attempts < maxAttempts) {
await delay(2000);
attempts++;
// 检查当前URL
const currentUrl = page.url();
// 如果URL变了,不再包含 passport,说明可能登录成功
if (!currentUrl.includes('passport.weibo.com')) {
console.log(`\n✅ 检测到页面跳转!URL: ${currentUrl}`);
loggedIn = true;
break;
}
// 每10秒提示一次
if (attempts % 5 === 0) {
console.log(`⏳ 等待扫码中... (${attempts * 2}秒)`);
}
}
if (loggedIn) {
// ===== STEP 3: 保存 Cookie =====
console.log('\n📍 Step 3: 保存登录 Cookie...');
const cookies = await page.cookies();
fs.writeFileSync(COOKIE_FILE, JSON.stringify(cookies, null, 2));
console.log(`💾 Cookie 已保存到: ${COOKIE_FILE}`);
console.log(`📊 共 ${cookies.length} 个 Cookie`);
// 显示关键 Cookie
const keyCookies = cookies.filter(c =>
['SUB', 'SUBP', 'ALF', 'SCF', 'SSOLoginState'].includes(c.name)
);
console.log('\n🔑 关键 Cookie:');
keyCookies.forEach(c => {
console.log(` - ${c.name}: ${c.value.substring(0, 30)}...`);
});
// 验证登录状态
await page.goto('https://weibo.com/u/me', { waitUntil: 'networkidle2' });
await delay(3000);
const verifyScreenshot = `${SCREENSHOT_DIR}/logged_in.png`;
await page.screenshot({ path: verifyScreenshot });
fs.copyFileSync(verifyScreenshot, `/home/ubuntu/nanocode/audiobook-player/static/logged_in.png`);
console.log('\n📸 验证截图: http://129.211.173.87:8899/screenshots/logged_in.png');
console.log('\n✅ 全部完成!Cookie 已保存,可以发邮件给你');
} else {
console.log('\n⏱️ 等待超时(120秒),可能未扫码');
}
} catch (error) {
console.error('❌ 错误:', error.message);
} finally {
await browser.close();
}
}
stepByStepLogin().catch(console.error);
@@ -0,0 +1,90 @@
/**
* B站热搜抓取
* https://s.search.bilibili.com/main/hotword
*/
const https = require('https');
const { saveData, formatHotNumber, printTable } = require('../utils/helpers');
const PLATFORM = 'bilibili';
/**
* HTTP GET 请求
*/
function httpGet(url) {
return new Promise((resolve, reject) => {
https.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://search.bilibili.com/',
}
}, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(new Error('JSON parse error: ' + e.message));
}
});
}).on('error', reject);
});
}
/**
* 抓取B站热搜
*/
async function fetchBilibiliTrending() {
console.log('📺 开始抓取B站热搜...');
try {
const apiUrl = 'https://s.search.bilibili.com/main/hotword';
const response = await httpGet(apiUrl);
if (response && response.code === 0 && response.list) {
const items = response.list.map((item, index) => ({
rank: index + 1,
title: item.keyword,
showName: item.show_name,
hot: item.icon ? '热' : '',
heatScore: item.heat_score || 0,
url: `https://search.bilibili.com/all?keyword=${encodeURIComponent(item.keyword)}`,
}));
const result = {
platform: PLATFORM,
timestamp: new Date().toISOString(),
url: 'https://www.bilibili.com',
count: items.length,
items: items.slice(0, 20),
};
// 打印结果
printTable(result.items.map(i => ({
rank: i.rank,
title: i.title,
hot: i.hot + (i.heatScore ? ` ${formatHotNumber(i.heatScore)}` : '')
})));
// 保存数据
saveData(PLATFORM, result);
console.log(`✅ 成功抓取 ${items.length} 条B站热搜`);
return result;
} else {
throw new Error('API 返回格式异常: ' + (response.message || 'unknown'));
}
} catch (error) {
console.error('❌ 抓取失败:', error.message);
throw error;
}
}
module.exports = { fetchBilibiliTrending };
// 直接运行
if (require.main === module) {
fetchBilibiliTrending().catch(console.error);
}
@@ -0,0 +1,194 @@
/**
* 博客园热门/搜索抓取
* https://www.cnblogs.com/
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../../../config/browser');
const { saveData, randomDelay, printTable } = require('../../utils/helpers');
const PLATFORM = 'cnblogs';
/**
* 抓取博客园热门文章
*/
async function fetchCnblogsHot() {
console.log('💻 开始抓取博客园热门文章...');
const browser = await puppeteer.launch(getLaunchOptions());
try {
const page = await browser.newPage();
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
// 访问博客园首页
await page.goto('https://www.cnblogs.com/', {
waitUntil: 'networkidle2',
timeout: 60000,
});
await randomDelay(2000, 3000);
// 提取文章数据
const items = await page.evaluate(() => {
const results = [];
// 博客园文章卡片选择器
const selectors = [
'.post-item .post-item-title a',
'.article-item .title a',
'#post_list .post_item .titlelnk',
'.entrylist .entrylistItem .entrylistTitle a',
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
if (elements.length > 0) {
elements.forEach((el, index) => {
const title = el.textContent?.trim();
const href = el.getAttribute('href');
if (title && href && title.length > 5) {
results.push({
rank: results.length + 1,
title: title.substring(0, 100),
url: href.startsWith('http') ? href : `https:${href}`,
});
}
});
break;
}
}
return results;
});
const result = {
platform: PLATFORM,
timestamp: new Date().toISOString(),
url: 'https://www.cnblogs.com/',
count: items.length,
items: items.slice(0, 20),
};
if (items.length > 0) {
printTable(result.items);
saveData(PLATFORM, result);
console.log(`✅ 成功抓取 ${items.length} 条博客园文章`);
} else {
console.log('⚠️ 未解析到文章');
}
return result;
} catch (error) {
console.error('❌ 抓取失败:', error.message);
throw error;
} finally {
await browser.close();
}
}
/**
* 搜索博客园文章
* @param {string} keyword - 搜索关键词
*/
async function searchCnblogs(keyword) {
console.log(`🔍 搜索博客园: "${keyword}"...`);
const browser = await puppeteer.launch(getLaunchOptions());
try {
const page = await browser.newPage();
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
// 访问搜索页面
const searchUrl = `https://zzk.cnblogs.com/s?w=${encodeURIComponent(keyword)}`;
await page.goto(searchUrl, {
waitUntil: 'networkidle2',
timeout: 60000,
});
await randomDelay(2000, 3000);
// 提取搜索结果
const items = await page.evaluate(() => {
const results = [];
// 搜索结果选择器
const selectors = [
'.searchItem .searchItemTitle a',
'.search-item .title a',
'.article-list .article-item a',
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
if (elements.length > 0) {
elements.forEach((el) => {
const title = el.textContent?.trim();
const href = el.getAttribute('href');
if (title && href && title.length > 5) {
// 去重
if (!results.find(r => r.title === title)) {
results.push({
rank: results.length + 1,
title: title.substring(0, 100),
url: href.startsWith('http') ? href : `https:${href}`,
});
}
}
});
break;
}
}
return results;
});
const result = {
platform: PLATFORM,
keyword,
timestamp: new Date().toISOString(),
url: searchUrl,
count: items.length,
items: items.slice(0, 20),
};
if (items.length > 0) {
printTable(result.items);
saveData(`${PLATFORM}_search`, result);
console.log(`✅ 成功搜索到 ${items.length} 条博客园文章`);
} else {
console.log('⚠️ 未找到相关文章');
}
return result;
} catch (error) {
console.error('❌ 搜索失败:', error.message);
throw error;
} finally {
await browser.close();
}
}
module.exports = { fetchCnblogsHot, searchCnblogs };
// 直接运行
if (require.main === module) {
const keyword = process.argv[2];
if (keyword) {
searchCnblogs(keyword).catch(console.error);
} else {
fetchCnblogsHot().catch(console.error);
}
}
@@ -0,0 +1,198 @@
/**
* 掘金热门文章抓取(使用 Puppeteer)
* https://juejin.cn/hot/articles
* 更新: 2026-03-28 修复选择器
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../../../config/browser');
const { saveData, randomDelay, printTable } = require('../../utils/helpers');
const PLATFORM = 'juejin';
/**
* 抓取掘金热门文章
*/
async function fetchJuejinHot(category = 'all') {
console.log(`📱 开始抓取掘金热门文章...`);
const browser = await puppeteer.launch(getLaunchOptions());
try {
const page = await browser.newPage();
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
// 打开掘金热榜
await page.goto('https://juejin.cn/hot/articles', {
waitUntil: 'networkidle2',
timeout: 60000,
});
await randomDelay(3000, 5000);
// 提取文章数据(2026-03-28 更新选择器)
const items = await page.evaluate(() => {
const results = [];
const seen = new Set();
// 查找所有包含 /post/ 的链接
const allLinks = document.querySelectorAll('a[href*="/post/"]');
allLinks.forEach((el) => {
const href = el.getAttribute('href');
const text = el.textContent?.trim();
// 提取文章ID
const match = href.match(/\/post\/(\d+)/);
if (!match) return;
const articleId = match[1];
// 跳过重复
if (seen.has(articleId)) return;
seen.add(articleId);
// 清理标题(去除作者名、数字等)
let title = text
?.replace(/\d+\s*$/, '') // 去除末尾数字
?.replace(/\s{2,}/g, ' ') // 合并多余空格
?.trim();
// 过滤无效标题
if (!title || title.length < 10 || title.length > 100) return;
if (title.match(/^\d+$/) || title.includes('http')) return;
results.push({
rank: results.length + 1,
title,
url: `https://juejin.cn/post/${articleId}`,
});
});
return results;
});
const result = {
platform: PLATFORM,
category,
timestamp: new Date().toISOString(),
url: 'https://juejin.cn/hot/articles',
count: items.length,
items: items.slice(0, 20),
};
if (items.length > 0) {
printTable(result.items);
saveData(PLATFORM, result);
console.log(`✅ 成功抓取 ${items.length} 条掘金文章`);
} else {
console.log('⚠️ 未解析到文章,页面结构可能已变化');
}
return result;
} catch (error) {
console.error('❌ 抓取失败:', error.message);
throw error;
} finally {
await browser.close();
}
}
/**
* 搜索掘金文章
* @param {string} keyword - 搜索关键词
*/
async function searchJuejin(keyword) {
console.log(`🔍 搜索掘金: "${keyword}"...`);
const browser = await puppeteer.launch(getLaunchOptions());
try {
const page = await browser.newPage();
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
const searchUrl = `https://juejin.cn/search?query=${encodeURIComponent(keyword)}&type=0`;
await page.goto(searchUrl, {
waitUntil: 'networkidle2',
timeout: 60000,
});
await randomDelay(3000, 5000);
const items = await page.evaluate(() => {
const results = [];
const seen = new Set();
const allLinks = document.querySelectorAll('a[href*="/post/"]');
allLinks.forEach((el) => {
const text = el.textContent?.trim();
const href = el.getAttribute('href');
const match = href.match(/\/post\/(\d+)/);
if (!match) return;
const articleId = match[1];
if (seen.has(articleId)) return;
seen.add(articleId);
let title = text
?.replace(/\d+\s*$/, '')
?.replace(/\s{2,}/g, ' ')
?.trim();
if (!title || title.length < 10 || title.length > 100) return;
if (title.match(/^\d+$/) || title.includes('http')) return;
results.push({
rank: results.length + 1,
title,
url: `https://juejin.cn/post/${articleId}`,
});
});
return results;
});
const result = {
platform: PLATFORM,
keyword,
timestamp: new Date().toISOString(),
url: searchUrl,
count: items.length,
items: items.slice(0, 20),
};
printTable(result.items);
saveData(`${PLATFORM}_search`, result);
console.log(`✅ 成功搜索到 ${items.length} 条掘金文章`);
return result;
} catch (error) {
console.error('❌ 搜索失败:', error.message);
throw error;
} finally {
await browser.close();
}
}
module.exports = { fetchJuejinHot, searchJuejin };
// 直接运行
if (require.main === module) {
const keyword = process.argv[2];
if (keyword) {
searchJuejin(keyword).catch(console.error);
} else {
fetchJuejinHot().catch(console.error);
}
}
@@ -0,0 +1,98 @@
/**
* 微博热搜抓取
* https://s.weibo.com/top/summary
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../../config/browser');
const { saveData, formatHotNumber, randomDelay, printTable } = require('../utils/helpers');
const PLATFORM = 'weibo';
const URL = 'https://s.weibo.com/top/summary';
/**
* 抓取微博热搜
*/
async function fetchWeiboHot() {
const browser = await puppeteer.launch(getLaunchOptions());
try {
console.log('🔥 开始抓取微博热搜...');
const page = await browser.newPage();
// 设置 User-Agent
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
// 打开页面
await page.goto(URL, {
waitUntil: 'networkidle2',
timeout: 60000
});
await randomDelay(1000, 2000);
// 提取热搜数据
const hotspots = await page.evaluate(() => {
const items = [];
const rows = document.querySelectorAll('#pl_top_realtimehot tbody tr');
rows.forEach((row, index) => {
// 跳过表头
if (index === 0) return;
const rankEl = row.querySelector('.td-01.ranktop');
const titleEl = row.querySelector('.td-02 a');
const hotEl = row.querySelector('.td-02 span');
if (titleEl) {
const rank = rankEl ? rankEl.textContent.trim() : String(index);
const title = titleEl.textContent.trim();
const hot = hotEl ? hotEl.textContent.trim() : '';
const url = titleEl.href;
items.push({
rank: parseInt(rank) || index,
title,
hot,
url,
});
}
});
return items;
});
const result = {
platform: PLATFORM,
timestamp: new Date().toISOString(),
url: URL,
count: hotspots.length,
items: hotspots.slice(0, 20), // 只取前20条
};
// 打印结果
printTable(result.items);
// 保存数据
saveData(PLATFORM, result);
console.log(`✅ 成功抓取 ${hotspots.length} 条微博热搜`);
return result;
} catch (error) {
console.error('❌ 抓取失败:', error.message);
throw error;
} finally {
await browser.close();
}
}
module.exports = { fetchWeiboHot };
// 直接运行
if (require.main === module) {
fetchWeiboHot().catch(console.error);
}
@@ -0,0 +1,96 @@
/**
* 小红书搜索/热点抓取
* 注意:小红书需要登录才能获取完整数据
*/
const puppeteer = require('puppeteer-core');
const { getLaunchOptions } = require('../../config/browser');
const { saveData, randomDelay, printTable } = require('../utils/helpers');
const PLATFORM = 'xiaohongshu';
/**
* 抓取小红书搜索建议/热点
* @param {string} keyword - 搜索关键词
*/
async function fetchXiaohongshuSearch(keyword = 'AI') {
const browser = await puppeteer.launch(getLaunchOptions());
try {
console.log(`🔴 开始抓取小红书搜索: "${keyword}"...`);
const page = await browser.newPage();
// 设置 User-Agent
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
);
// 小红书搜索页
const searchUrl = `https://www.xiaohongshu.com/search_result?keyword=${encodeURIComponent(keyword)}`;
await page.goto(searchUrl, {
waitUntil: 'networkidle2',
timeout: 60000
});
await randomDelay(2000, 4000);
// 尝试提取笔记数据
const notes = await page.evaluate(() => {
const items = [];
// 小红书的笔记卡片选择器(可能随时间变化)
const cards = document.querySelectorAll('.note-item, [class*="note"], .feeds-page > div > div');
cards.forEach((card, index) => {
const titleEl = card.querySelector('.title, .desc, [class*="title"]');
const linkEl = card.querySelector('a[href*="/explore/"]');
if (titleEl) {
items.push({
rank: index + 1,
title: titleEl.textContent.trim().substring(0, 100),
url: linkEl ? linkEl.href : '',
});
}
});
return items;
});
const result = {
platform: PLATFORM,
keyword,
timestamp: new Date().toISOString(),
url: searchUrl,
count: notes.length,
items: notes.slice(0, 10),
note: '小红书需要登录才能获取完整数据',
};
if (notes.length > 0) {
printTable(result.items);
} else {
console.log('⚠️ 未获取到数据,可能需要登录');
}
saveData(PLATFORM, result);
console.log(`✅ 完成`);
return result;
} catch (error) {
console.error('❌ 抓取失败:', error.message);
throw error;
} finally {
await browser.close();
}
}
module.exports = { fetchXiaohongshuSearch };
// 直接运行
if (require.main === module) {
const keyword = process.argv[2] || 'AI';
fetchXiaohongshuSearch(keyword).catch(console.error);
}
+163
View File
@@ -0,0 +1,163 @@
/**
* 知乎热榜抓取(支持 Cookie)
* https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total
*/
const https = require('https');
const fs = require('fs');
const path = require('path');
const { saveData, formatHotNumber, printTable } = require('../utils/helpers');
const PLATFORM = 'zhihu';
const COOKIE_FILE = '/home/ubuntu/zhihu_cookies.txt';
/**
* 解析 Cookie 文件
* 支持格式1: JSON (从浏览器插件导出)
* 支持格式2: 纯文本 name=value; name2=value2
*/
function loadCookies() {
try {
if (!fs.existsSync(COOKIE_FILE)) {
console.warn(`⚠️ Cookie 文件不存在: ${COOKIE_FILE}`);
return null;
}
const content = fs.readFileSync(COOKIE_FILE, 'utf-8').trim();
// 尝试解析为 JSON
if (content.startsWith('[') || content.startsWith('{')) {
try {
const cookies = JSON.parse(content);
// 处理数组格式 [{name, value, domain, ...}, ...]
if (Array.isArray(cookies)) {
const cookieStr = cookies
.filter(c => c.name && c.value)
.map(c => `${c.name}=${c.value}`)
.join('; ');
console.log(`📋 已加载 JSON Cookie: ${cookies.length} 条`);
return cookieStr;
}
// 处理对象格式 {name: value, name2: value2}
else if (typeof cookies === 'object') {
const cookieStr = Object.entries(cookies)
.map(([k, v]) => `${k}=${v}`)
.join('; ');
console.log(`📋 已加载 JSON Object Cookie`);
return cookieStr;
}
} catch (e) {
// JSON 解析失败,当作纯文本处理
}
}
// 纯文本格式
console.log(`📋 已加载文本 Cookie: ${content.length} 字符`);
return content;
} catch (e) {
console.warn('⚠️ 读取 Cookie 文件失败:', e.message);
return null;
}
}
/**
* HTTP GET 请求(带 Cookie)
*/
function httpGet(url, cookie = null) {
return new Promise((resolve, reject) => {
const headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://www.zhihu.com/',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
};
if (cookie) {
headers['Cookie'] = cookie;
}
https.get(url, { headers }, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (e) {
reject(new Error('JSON parse error: ' + e.message + '\nData: ' + data.substring(0, 200)));
}
});
}).on('error', reject);
});
}
/**
* 抓取知乎热榜
*/
async function fetchZhihuHot() {
console.log('📚 开始抓取知乎热榜...');
// 加载 Cookie
const cookie = loadCookies();
if (!cookie) {
console.warn('⚠️ 未找到有效 Cookie,知乎抓取可能失败');
}
try {
const apiUrl = 'https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50';
const response = await httpGet(apiUrl, cookie);
// 检查是否需要登录
if (response.error) {
if (response.error.code === 101 || response.error.name === 'AuthenticationError') {
throw new Error('Cookie 已过期或无效,请更新 zhihu_cookies.txt');
}
throw new Error(`API 错误: ${response.error.message || JSON.stringify(response.error)}`);
}
if (response && response.data) {
const items = response.data.map((item, index) => {
const target = item.target || {};
const detailText = target.detail_text || ''; // "1234 万热度"
return {
rank: index + 1,
title: target.title,
excerpt: (target.excerpt || '').substring(0, 100),
hot: detailText,
url: `https://www.zhihu.com/question/${target.id}`,
};
});
const result = {
platform: PLATFORM,
timestamp: new Date().toISOString(),
url: 'https://www.zhihu.com/hot',
count: items.length,
items: items.slice(0, 20),
};
// 打印结果
printTable(result.items);
// 保存数据
saveData(PLATFORM, result);
console.log(`✅ 成功抓取 ${items.length} 条知乎热榜`);
return result;
} else {
throw new Error('API 返回格式异常: ' + JSON.stringify(response).substring(0, 200));
}
} catch (error) {
console.error('❌ 抓取失败:', error.message);
throw error;
}
}
module.exports = { fetchZhihuHot };
// 直接运行
if (require.main === module) {
fetchZhihuHot().catch(console.error);
}
+73
View File
@@ -0,0 +1,73 @@
/**
* 工具函数
*/
const fs = require('fs');
const path = require('path');
/**
* 保存数据到 JSON 文件
*/
function saveData(platform, data) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `${platform}_${timestamp}.json`;
const filepath = path.join(__dirname, '../../data', filename);
// 确保目录存在
const dir = path.dirname(filepath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filepath, JSON.stringify(data, null, 2), 'utf-8');
console.log(`💾 数据已保存: ${filepath}`);
return filepath;
}
/**
* 格式化热度数字
*/
function formatHotNumber(num) {
if (!num) return '';
if (num >= 10000) {
return (num / 10000).toFixed(1) + '万';
}
return num.toString();
}
/**
* 延迟函数
*/
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* 随机延迟(模拟人类行为)
*/
async function randomDelay(min = 1000, max = 3000) {
const ms = Math.floor(Math.random() * (max - min + 1)) + min;
await delay(ms);
}
/**
* 打印表格
*/
function printTable(items, columns = ['rank', 'title', 'hot']) {
console.log('\n' + '='.repeat(80));
items.forEach((item, i) => {
const rank = String(item.rank || i + 1).padStart(2, ' ');
const title = (item.title || item.keyword || 'N/A').substring(0, 50);
const hot = item.hot ? ` 🔥${item.hot}` : '';
console.log(`${rank}. ${title}${hot}`);
});
console.log('='.repeat(80) + '\n');
}
module.exports = {
saveData,
formatHotNumber,
delay,
randomDelay,
printTable,
};