GitHub Actions Bot commited on
Commit
2cff0cd
·
1 Parent(s): 4164c72

🚀 Auto-deploy: 7ba9ac65e52520c0b1375b69babf1f064d9da3bd

Browse files
Files changed (1) hide show
  1. README.md +209 -0
README.md ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Smart Search Crawler
3
+ emoji: 🕷️
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ # Smart Search Crawler - 多平台商品爬虫系统
11
+
12
+ 基于 **Crawlee** + **Playwright** 的高性能商品数据抓取工具,采用 TypeScript 编写,支持动态扩展多个电商平台。
13
+
14
+ ## ✨ 核心特性
15
+
16
+ - 🚀 **高性能并发**: 支持多线程并发爬取,智能资源拦截加速
17
+ - 🛡️ **防封禁策略**: 三种预设模式(快速/稳定/隐蔽)应对不同场景
18
+ - 🔌 **动态路由架构**: 通过 `source` 参数自动选择对应平台爬虫,易于扩展
19
+ - 📊 **灵活配置**: 集中化 CSS 选择器管理,支持运行时切换
20
+ - 💾 **多格式输出**: 支持 JSON 和 CSV 格式导出
21
+ - 🔧 **类型安全**: 完整的 TypeScript 类型定义
22
+ - 🌐 **REST API**: 简洁的 HTTP 接口,支持批量爬取
23
+ - ☁️ **多云部署**: 支持 Hugging Face Spaces、Render.com、VPS 等多种部署方式
24
+
25
+ ---
26
+
27
+ ## 🚀 快速开始
28
+
29
+ ### 1. 安装依赖
30
+
31
+ ```bash
32
+ npm install
33
+ ```
34
+
35
+ ### 2. 安装 Playwright 浏览器
36
+
37
+ ```bash
38
+ npx playwright install chromium
39
+ ```
40
+
41
+ **国内用户加速:**
42
+ ```powershell
43
+ # PowerShell
44
+ $env:PLAYWRIGHT_DOWNLOAD_HOST="https://npmmirror.com/mirrors/playwright/"
45
+ npx playwright install chromium
46
+ ```
47
+
48
+ ### 3. 启动 Web 服务
49
+
50
+ ```bash
51
+ npm run serve
52
+ ```
53
+
54
+ 服务将在 `http://localhost:7860` 启动。
55
+
56
+ ---
57
+
58
+ ## 📡 API 使用指南
59
+
60
+ ### 1. 健康检查
61
+
62
+ **请求:**
63
+ ```bash
64
+ curl http://localhost:7860/health
65
+ ```
66
+
67
+ **响应:**
68
+ ```json
69
+ {
70
+ "status": "ok",
71
+ "service": "smart-search-crawler",
72
+ "timestamp": "2024-04-20T14:00:00.000Z",
73
+ "supportedSources": ["Otto"]
74
+ }
75
+ ```
76
+
77
+ ### 2. 执行爬取任务
78
+
79
+ **请求:**
80
+ ```bash
81
+ curl -X POST http://localhost:7860/crawl \
82
+ -H "Content-Type: application/json" \
83
+ -d '{
84
+ "source": "Otto",
85
+ "urls": [
86
+ "https://www.otto.de/p/product-url-1",
87
+ "https://www.otto.de/p/product-url-2"
88
+ ],
89
+ "preset": "stable"
90
+ }'
91
+ ```
92
+
93
+ **参数说明:**
94
+
95
+ | 参数 | 类型 | 必填 | 说明 |
96
+ |------|------|------|------|
97
+ | `source` | string | ✅ | 爬虫源标识(如 `"Otto"`),系统根据此值动态路由到对应爬虫 |
98
+ | `urls` | string[] | ✅ | 待爬取的商品 URL 数组 |
99
+ | `preset` | string | ❌ | 预设模式:`fast` / `stable`(默认)/ `stealth` |
100
+
101
+ **响应示例:**
102
+ ```json
103
+ {
104
+ "success": true,
105
+ "source": "Otto",
106
+ "count": 2,
107
+ "data": [
108
+ {
109
+ "url": "https://www.otto.de/p/product-url-1",
110
+ "crawledAt": "2024-04-20T14:00:00.000Z",
111
+ "price": "279,99 €",
112
+ "rating": "4.5",
113
+ "reviews": "(434)",
114
+ "title": "Samsung Galaxy S23...",
115
+ "imageUrl": "https://..."
116
+ }
117
+ ]
118
+ }
119
+ ```
120
+
121
+ ---
122
+
123
+ ## ⚙️ 配置详解
124
+
125
+ ### 预设模式对比
126
+
127
+ | 模式 | 并发数 | 超时 | 重试次数 | 适用场景 |
128
+ |------|--------|------|----------|----------|
129
+ | **fast** | 20 | 30s | 1 | 快速测试、少量 URL |
130
+ | **stable** | 5 | 90s | 1 | 生产环境(✅ 推荐) |
131
+ | **stealth** | 3 | 180s | 1 | 大规模爬取、防封禁 |
132
+
133
+ ---
134
+
135
+ ## ☁️ Hugging Face Spaces 部署说明
136
+
137
+ 本 Space 已配置为 Docker 部署模式,具有以下特点:
138
+
139
+ ### 环境配置
140
+ - **服务器位置**: 欧洲(访问 Otto.de 等欧洲网站速度快)
141
+ - **端口**: 7860(HF 强制要求)
142
+ - **内存**: 16GB RAM
143
+ - **CPU**: 2 vCPU
144
+
145
+ ### 性能建议
146
+ - **推荐并发数**: 1-2(避免 OOM)
147
+ - **超时设置**: 120秒(适应跨国网络)
148
+ - **休眠策略**: 48小时无访问自动休眠
149
+
150
+ ### 使用示例
151
+
152
+ ```bash
153
+ # 测试健康检查
154
+ curl https://sky5678-crawler.hf.space/health
155
+
156
+ # 执行爬取任务
157
+ curl -X POST https://sky5678-crawler.hf.space/crawl \
158
+ -H "Content-Type: application/json" \
159
+ -d '{
160
+ "source": "Otto",
161
+ "urls": ["https://www.otto.de/p/example-product/"],
162
+ "preset": "stable"
163
+ }'
164
+ ```
165
+
166
+ ---
167
+
168
+ ## 🏗️ 项目结构
169
+
170
+ ```
171
+ crawler/
172
+ ├── src/
173
+ │ ├── config.ts # 全局配置(选择器、预设模式)
174
+ │ ├── otto-crawler.ts # Otto 平台核心爬虫实现
175
+ │ └── batch-crawler.ts # 批量处理与文件 I/O
176
+ ├── examples/
177
+ │ └── usage-examples.ts # 代码集成示例
178
+ ├── server.ts # Express REST API 服务
179
+ ├── quick-start.ts # 交互式命令行工具
180
+ ├── Dockerfile # Docker 容器化配置
181
+ └── .github/workflows/ # GitHub Actions CI/CD
182
+ ```
183
+
184
+ ---
185
+
186
+ ## ⚠️ 注意事项
187
+
188
+ 1. **遵守 robots.txt**: 请尊重目标网站的爬虫政策
189
+ 2. **控制频率**: 建议使用 `stable` 或 `stealth` 模式
190
+ 3. **网络优化**: HF Spaces 位于欧洲,访问欧洲电商网站延迟低
191
+ 4. **选择器维护**: 网站结构变更可能导致选择器失效
192
+
193
+ ---
194
+
195
+ ## 📄 许可证
196
+
197
+ MIT License
198
+
199
+ ---
200
+
201
+ ## 🤝 贡献
202
+
203
+ 欢迎提交 Issue 和 Pull Request!
204
+
205
+ **扩展新平台的步骤:**
206
+ 1. 在 `src/` 下创建新的爬虫文件
207
+ 2. 实现统一的 `ProductData[]` 返回接口
208
+ 3. 在 `server.ts` 的 `crawlerMap` 中注册
209
+ 4. 提交 PR 并添加测试用例