8f6f14da
tangwang
test data prepare:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
# 电商数据爬虫工具集
通过万邦API爬取电商平台(Shopee、Amazon)商品数据的工具集。
## 目录
- [Shopee爬虫](#shopee爬虫)
- [Amazon爬虫](#amazon爬虫)
- [通用说明](#通用说明)
---
# Shopee爬虫
通过万邦API爬取Shopee商品数据的简化脚本。
## 文件说明
```
data_crawling/
├── shopee_crawler.py # 主爬虫脚本
├── test_crawler.py # 测试脚本(爬取前5个)
├── queries.txt # 查询词列表(5024个)
├── 万邦API_shopee.md # API文档
├── shopee_results/ # 爬取结果目录
└── test_results/ # 测试结果目录
```
## 快速开始
### 1. 安装依赖
```bash
pip install requests
```
### 2. 测试运行(推荐先测试)
```bash
cd /home/tw/SearchEngine/data_crawling
python test_crawler.py
```
这会爬取前5个查询词,结果保存在 `test_results/` 目录。
### 3. 完整运行
```bash
python shopee_crawler.py
```
爬取全部5024个查询词,结果保存在 `shopee_results/` 目录。
## 配置参数
在脚本顶部可以修改:
```python
COUNTRY = '.com.my' # 站点: .vn, .co.th, .tw, .co.id, .sg, .com.my
PAGE = 1 # 页码
DELAY = 2 # 请求间隔(秒)
MAX_RETRIES = 3 # 最大重试次数
```
## 输出结果
### 文件命名
```
0001_Bohemian_Maxi_Dress_20231204_143025.json
0002_Vintage_Denim_Jacket_20231204_143028.json
...
```
### JSON格式
```json
{
"items": {
"keyword": "dress",
"total_results": 5000,
"item": [
{
"title": "商品标题",
"pic_url": "图片URL",
"price": 38.9,
"sales": 293,
"shop_id": "277113808",
"detail_url": "商品链接"
}
]
},
"error_code": "0000",
"reason": "ok"
}
```
### 摘要文件
`summary.json` 包含爬取统计:
```json
{
"crawl_time": "2023-12-04T14:30:25",
"total": 5024,
"success": 5000,
"fail": 24,
"elapsed_seconds": 10248,
"failed_queries": ["query1", "query2"]
}
```
## 预估时间
- 单个查询: ~4秒(含2秒延迟)
- 全部5024个: ~5.6小时
## API信息
- **地址**: `https://api-gw.onebound.cn/shopee/item_search`
- **Key**: `t8618339029`
- **Secret**: `9029f568`
- **每日限额**: 10,000次
## 常用命令
```bash
# 测试(前5个)
python test_crawler.py
# 完整爬取
python shopee_crawler.py
# 查看结果数量
ls shopee_results/*.json | wc -l
# 查看失败的查询
cat shopee_results/failed_queries.txt
# 查看摘要
cat shopee_results/summary.json
```
## 注意事项
1. **API限额**: 每日最多10,000次调用
2. **请求间隔**: 建议保持2秒以上
3. **网络稳定**: 需要稳定的网络连接
4. **存储空间**: 确保有足够的磁盘空间(约2-3GB)
## 故障排除
**问题**: API返回错误
- 检查网络连接
- 确认API配额未用完
- 查看 `failed_queries.txt`
**问题**: 文件编码错误
- 脚本已使用UTF-8编码,无需额外设置
**问题**: 中断后继续
- 可以删除已爬取的JSON文件对应的查询词,重新运行
---
# Amazon爬虫
通过万邦API爬取Amazon商品数据的脚本。
## 快速开始
### 1. 配置API密钥
#### 方法1:使用配置文件(推荐)
```bash
cd /home/tw/SearchEngine/data_crawling
cp config.example.py config.py
# 编辑 config.py,填入你的API密钥
```
#### 方法2:使用命令行参数
```bash
python amazon_crawler_v2.py --key YOUR_KEY --secret YOUR_SECRET
```
#### 方法3:使用环境变量
```bash
export ONEBOUND_API_KEY="your_key_here"
export ONEBOUND_API_SECRET="your_secret_here"
```
### 2. 测试API连接
```bash
python test_api.py
```
这会测试API连接是否正常,并显示配额信息。
### 3. 开始爬取
#### 测试模式(前10个查询)
```bash
python amazon_crawler_v2.py --max 10
```
#### 完整爬取(全部5024个查询)
```bash
python amazon_crawler_v2.py
```
## 脚本说明
### amazon_crawler.py
基础版爬虫,需要在代码中配置API密钥。
### amazon_crawler_v2.py(推荐)
增强版爬虫,支持:
- 配置文件/命令行参数/环境变量
- 详细的日志输出
- 进度显示
- 统计信息
- 断点续爬
### test_api.py
API连接测试工具,用于验证:
- API密钥是否正确
- 网络连接是否正常
- API配额是否充足
### analyze_results.py
结果分析工具,用于:
- 统计爬取结果
- 分析商品数据(价格、评分等)
- 导出CSV格式
## 命令行参数
```bash
python amazon_crawler_v2.py [选项]
选项:
--key KEY API Key
--secret SECRET API Secret
--queries FILE 查询文件路径(默认:queries.txt)
--delay SECONDS 请求间隔(默认:2.0秒)
--start INDEX 起始索引,用于断点续爬(默认:0)
--max NUM 最大爬取数量(默认:全部)
--output DIR 结果保存目录(默认:amazon_results)
```
## 使用示例
### 测试前10个查询
```bash
python amazon_crawler_v2.py --max 10
```
### 从第100个查询继续爬取
```bash
python amazon_crawler_v2.py --start 100
```
### 使用自定义延迟
```bash
python amazon_crawler_v2.py --delay 1.5
```
### 分析爬取结果
```bash
# 显示统计信息
python analyze_results.py
# 导出为CSV
python analyze_results.py --csv
# 指定结果目录
python analyze_results.py --dir amazon_results
```
## 输出结果
### 文件命名
```
0001_Bohemian_Maxi_Dress.json
0002_Vintage_Denim_Jacket.json
0003_Minimalist_Linen_Trousers.json
...
```
### JSON格式
```json
{
"items": {
"item": [
{
"detail_url": "https://www.amazon.com/...",
"num_iid": "B07F8S18D5",
"pic_url": "https://...",
"price": "9.99",
"reviews": "53812",
"sales": 10000,
"stars": "4.7",
"title": "商品标题"
}
],
"page": "1",
"page_size": 100,
"real_total_results": 700,
"q": "搜索词"
},
"error_code": "0000",
"reason": "ok"
}
```
### 日志文件
运行日志保存在 `amazon_crawler.log`:
```
2025-01-07 10:00:00 - INFO - Amazon爬虫启动
2025-01-07 10:00:01 - INFO - [1/5024] (0.0%) - Bohemian Maxi Dress
2025-01-07 10:00:02 - INFO - ✓ 成功: Bohemian Maxi Dress - 获得 700 个结果
...
```
### 分析报告
运行分析工具后生成 `analysis_report.json`:
```json
{
"total_files": 5024,
"successful": 5000,
"failed": 24,
"success_rate": "99.5%",
"total_items": 50000,
"avg_items_per_query": 10.0
}
```
## 预估时间
- 单个查询: ~4秒(含2秒延迟)
- 全部5024个: ~5.6小时
## 注意事项
1. **API限额**: 请注意API的每日/每月配额限制
2. **请求间隔**: 建议保持2秒以上的延迟
3. **断点续爬**: 使用 `--start` 参数可以从指定位置继续
4. **磁盘空间**: 确保有足够的存储空间(约2-3GB)
## 详细文档
更多详细信息,请查看:
- [Amazon爬虫详细文档](AMAZON_CRAWLER_README.md)
- [API文档](万邦API_亚马逊.md)
---
# 通用说明
## 文件结构
```
data_crawling/
├── shopee_crawler.py # Shopee爬虫
├── test_crawler.py # Shopee测试脚本
├── amazon_crawler.py # Amazon爬虫(基础版)
├── amazon_crawler_v2.py # Amazon爬虫(增强版)
├── test_api.py # API测试工具
├── analyze_results.py # 结果分析工具
├── config.example.py # 配置文件示例
├── queries.txt # 查询词列表(5024个)
├── 万邦API_shopee.md # Shopee API文档
├── 万邦API_亚马逊.md # Amazon API文档
├── AMAZON_CRAWLER_README.md # Amazon爬虫详细文档
├── shopee_results/ # Shopee结果目录
├── amazon_results/ # Amazon结果目录
└── test_results/ # 测试结果目录
```
## 依赖安装
```bash
pip install requests
```
## 常见问题
### 1. API密钥错误
**错误**: `错误: 未配置API密钥!`
**解决**:
- 检查配置文件是否正确
- 使用 `test_api.py` 测试连接
- 确认密钥没有多余的空格或引号
### 2. 请求超时
**错误**: `请求失败: timeout`
**解决**:
- 检查网络连接
- 尝试增加延迟时间
- 检查API服务是否正常
### 3. API配额耗尽
**错误**: `quota exceeded`
**解决**:
- 查看日志中的 `api_info` 字段
- 等待配额重置
- 使用 `--max` 参数限制爬取数量
### 4. 中断后继续爬取
使用 `--start` 参数指定起始位置:
```bash
# 从第1000个查询继续
python amazon_crawler_v2.py --start 1000
```
## 许可证
本项目仅供学习和研究使用。使用API时请遵守相应平台的服务条款。
|