# nodejs **Repository Path**: learning-section/nodejs ## Basic Information - **Project Name**: nodejs - **Description**: nodejs 实现抓包 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-28 - **Last Updated**: 2026-01-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Node.js 网页爬虫 API 服务 ## 项目描述 这是一个基于 Node.js 和 Express 的多功能网页爬虫 API 服务,提供以下功能: - **新闻爬虫接口**: 从多个新闻源(HackerNews、TechCrunch、Medium)获取最新新闻 - **商品爬虫接口**: 爬取商品信息、支持搜索和分类过滤 - **天气数据接口**: 获取多个城市的天气信息和预报 ## 目录结构 ``` nodejs/ ├── src/ │ ├── app.js # 主应用文件 │ ├── routes/ # 路由目录 │ │ ├── news.js # 新闻接口路由 │ │ ├── product.js # 商品接口路由 │ │ └── weather.js # 天气接口路由 │ └── scrapers/ # 爬虫逻辑目录 │ ├── newsScraper.js # 新闻爬虫 │ ├── productScraper.js # 商品爬虫 │ └── weatherScraper.js # 天气爬虫 ├── package.json # 项目配置 ├── .env # 环境变量 └── .gitignore # Git 忽略文件 ``` ## API 接口文档 ### 基础路由 #### 获取 API 信息 ``` GET / ``` **响应:** ```json { "message": "欢迎使用网页爬虫 API", "version": "1.0.0", "endpoints": { "news": "/api/news", "product": "/api/product", "weather": "/api/weather", "health": "/health" } } ``` #### 健康检查 ``` GET /health ``` ### 新闻接口 (`/api/news`) #### 获取新闻列表 ``` GET /api/news?source=hackernews ``` **查询参数:** - `source` (可选) - 新闻来源:`hackernews`, `techcrunch`, `medium` (默认: hackernews) **响应示例:** ```json { "success": true, "data": [ { "id": 1, "title": "News Title", "url": "https://example.com" } ], "source": "hackernews", "count": 10, "timestamp": "2026-01-28T10:00:00.000Z" } ``` #### 获取特定新闻详情 ``` GET /api/news/:id ``` #### 批量获取多个新闻源 ``` POST /api/news/batch ``` **请求体:** ```json { "sources": ["hackernews", "techcrunch", "medium"] } ``` ### 商品接口 (`/api/product`) #### 获取商品列表 ``` GET /api/product?category=electronics ``` **查询参数:** - `category` (可选) - 商品分类 (默认: electronics) #### 获取商品详情 ``` GET /api/product/:id ``` #### 搜索商品 ``` GET /api/product/search/:keyword ``` #### 批量获取商品信息 ``` POST /api/product/batch ``` **请求体:** ```json { "ids": [1, 2, 3] } ``` ### 天气接口 (`/api/weather`) #### 获取指定城市天气 ``` GET /api/weather/:city ``` **支持的城市:** beijing, shanghai, guangzhou **响应示例:** ```json { "success": true, "data": { "city": "北京", "temp": 5, "condition": "晴", "humidity": 45, "windSpeed": 10, "uvIndex": 3, "timestamp": "2026-01-28T10:00:00.000Z", "forecast": [ { "day": "tomorrow", "high": 12, "low": 5, "condition": "晴" } ] } } ``` #### 获取所有支持城市天气 ``` GET /api/weather ``` #### 批量获取多个城市天气 ``` POST /api/weather/batch ``` **请求体:** ```json { "cities": ["beijing", "shanghai", "guangzhou"] } ``` ## 安装和运行 ### 前置要求 - Node.js 14.0 以上版本 - npm 或 yarn ### 安装依赖 ```bash npm install ``` ### 开发模式运行 ```bash npm run dev ``` ### 生产环境运行 ```bash npm start ``` 服务器将在 `http://localhost:3000` 上运行 ## 环境变量配置 编辑 `.env` 文件来配置: ``` PORT=3000 # 服务器端口 NODE_ENV=development # 运行环境 (development/production) LOG_LEVEL=info # 日志级别 ``` ## 测试 API ### 使用 curl ```bash # 获取新闻 curl http://localhost:3000/api/news # 获取商品 curl http://localhost:3000/api/product # 获取天气 curl http://localhost:3000/api/weather/beijing # 批量获取天气 curl -X POST http://localhost:3000/api/weather/batch \ -H "Content-Type: application/json" \ -d '{"cities": ["beijing", "shanghai"]}' ``` ### 使用 Postman 1. 导入以下请求到 Postman 2. 修改 URL 和请求体 3. 点击发送 ## 依赖说明 - **express**: Web 框架 - **axios**: HTTP 请求库,用于爬虫数据获取 - **cheerio**: jQuery 语法解析 HTML,用于页面解析 - **cors**: 跨域资源共享中间件 - **body-parser**: 请求体解析中间件 - **dotenv**: 环境变量加载 - **nodemon**: 开发环境下自动重启服务 ## 错误处理 所有 API 响应遵循以下格式: **成功响应:** ```json { "success": true, "data": { ... } } ``` **错误响应:** ```json { "success": false, "error": "错误信息" } ``` ## 性能优化建议 1. **缓存**: 使用 Redis 缓存频繁请求的数据 2. **限流**: 使用 express-rate-limit 限制请求速率 3. **日志**: 使用 Winston 或 Bunyan 管理日志 4. **监控**: 使用 PM2 监控和管理进程 5. **数据库**: 使用 MongoDB 或 PostgreSQL 存储爬虫数据 ## 扩展功能 - 添加数据库支持持久化存储爬虫数据 - 实现任务调度,定期更新爬虫数据 - 添加认证和授权机制 - 实现数据导出功能(CSV、JSON) - 添加可视化仪表板 ## 许可证 MIT