crawlo 1.2.3__py3-none-any.whl → 1.2.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of crawlo might be problematic. Click here for more details.

Files changed (222) hide show
  1. crawlo/__init__.py +61 -61
  2. crawlo/__version__.py +1 -1
  3. crawlo/cleaners/__init__.py +60 -60
  4. crawlo/cleaners/data_formatter.py +225 -225
  5. crawlo/cleaners/encoding_converter.py +125 -125
  6. crawlo/cleaners/text_cleaner.py +232 -232
  7. crawlo/cli.py +81 -81
  8. crawlo/commands/__init__.py +14 -14
  9. crawlo/commands/check.py +594 -594
  10. crawlo/commands/genspider.py +151 -151
  11. crawlo/commands/help.py +144 -142
  12. crawlo/commands/list.py +155 -155
  13. crawlo/commands/run.py +323 -292
  14. crawlo/commands/startproject.py +420 -417
  15. crawlo/commands/stats.py +187 -187
  16. crawlo/commands/utils.py +186 -186
  17. crawlo/config.py +312 -312
  18. crawlo/config_validator.py +251 -251
  19. crawlo/core/__init__.py +2 -2
  20. crawlo/core/engine.py +354 -354
  21. crawlo/core/processor.py +40 -40
  22. crawlo/core/scheduler.py +143 -143
  23. crawlo/crawler.py +1110 -1027
  24. crawlo/data/__init__.py +5 -5
  25. crawlo/data/user_agents.py +107 -107
  26. crawlo/downloader/__init__.py +266 -266
  27. crawlo/downloader/aiohttp_downloader.py +220 -220
  28. crawlo/downloader/cffi_downloader.py +256 -256
  29. crawlo/downloader/httpx_downloader.py +259 -259
  30. crawlo/downloader/hybrid_downloader.py +212 -212
  31. crawlo/downloader/playwright_downloader.py +402 -402
  32. crawlo/downloader/selenium_downloader.py +472 -472
  33. crawlo/event.py +11 -11
  34. crawlo/exceptions.py +81 -81
  35. crawlo/extension/__init__.py +37 -37
  36. crawlo/extension/health_check.py +141 -141
  37. crawlo/extension/log_interval.py +57 -57
  38. crawlo/extension/log_stats.py +81 -81
  39. crawlo/extension/logging_extension.py +43 -43
  40. crawlo/extension/memory_monitor.py +104 -104
  41. crawlo/extension/performance_profiler.py +133 -133
  42. crawlo/extension/request_recorder.py +107 -107
  43. crawlo/filters/__init__.py +154 -154
  44. crawlo/filters/aioredis_filter.py +280 -280
  45. crawlo/filters/memory_filter.py +269 -269
  46. crawlo/items/__init__.py +23 -23
  47. crawlo/items/base.py +21 -21
  48. crawlo/items/fields.py +52 -52
  49. crawlo/items/items.py +104 -104
  50. crawlo/middleware/__init__.py +21 -21
  51. crawlo/middleware/default_header.py +131 -131
  52. crawlo/middleware/download_delay.py +104 -104
  53. crawlo/middleware/middleware_manager.py +135 -135
  54. crawlo/middleware/offsite.py +114 -114
  55. crawlo/middleware/proxy.py +367 -367
  56. crawlo/middleware/request_ignore.py +86 -86
  57. crawlo/middleware/response_code.py +163 -163
  58. crawlo/middleware/response_filter.py +136 -136
  59. crawlo/middleware/retry.py +124 -124
  60. crawlo/mode_manager.py +211 -211
  61. crawlo/network/__init__.py +21 -21
  62. crawlo/network/request.py +338 -338
  63. crawlo/network/response.py +359 -359
  64. crawlo/pipelines/__init__.py +21 -21
  65. crawlo/pipelines/bloom_dedup_pipeline.py +156 -156
  66. crawlo/pipelines/console_pipeline.py +39 -39
  67. crawlo/pipelines/csv_pipeline.py +316 -316
  68. crawlo/pipelines/database_dedup_pipeline.py +222 -222
  69. crawlo/pipelines/json_pipeline.py +218 -218
  70. crawlo/pipelines/memory_dedup_pipeline.py +115 -115
  71. crawlo/pipelines/mongo_pipeline.py +131 -131
  72. crawlo/pipelines/mysql_pipeline.py +317 -317
  73. crawlo/pipelines/pipeline_manager.py +61 -61
  74. crawlo/pipelines/redis_dedup_pipeline.py +165 -165
  75. crawlo/project.py +279 -187
  76. crawlo/queue/pqueue.py +37 -37
  77. crawlo/queue/queue_manager.py +337 -337
  78. crawlo/queue/redis_priority_queue.py +298 -298
  79. crawlo/settings/__init__.py +7 -7
  80. crawlo/settings/default_settings.py +217 -226
  81. crawlo/settings/setting_manager.py +122 -122
  82. crawlo/spider/__init__.py +639 -639
  83. crawlo/stats_collector.py +59 -59
  84. crawlo/subscriber.py +129 -129
  85. crawlo/task_manager.py +30 -30
  86. crawlo/templates/crawlo.cfg.tmpl +10 -10
  87. crawlo/templates/project/__init__.py.tmpl +3 -3
  88. crawlo/templates/project/items.py.tmpl +17 -17
  89. crawlo/templates/project/middlewares.py.tmpl +118 -118
  90. crawlo/templates/project/pipelines.py.tmpl +96 -96
  91. crawlo/templates/project/run.py.tmpl +47 -45
  92. crawlo/templates/project/settings.py.tmpl +350 -325
  93. crawlo/templates/project/settings_distributed.py.tmpl +160 -121
  94. crawlo/templates/project/settings_gentle.py.tmpl +133 -94
  95. crawlo/templates/project/settings_high_performance.py.tmpl +155 -151
  96. crawlo/templates/project/settings_simple.py.tmpl +108 -68
  97. crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
  98. crawlo/templates/spider/spider.py.tmpl +143 -143
  99. crawlo/tools/__init__.py +182 -182
  100. crawlo/tools/anti_crawler.py +268 -268
  101. crawlo/tools/authenticated_proxy.py +240 -240
  102. crawlo/tools/data_validator.py +180 -180
  103. crawlo/tools/date_tools.py +35 -35
  104. crawlo/tools/distributed_coordinator.py +386 -386
  105. crawlo/tools/retry_mechanism.py +220 -220
  106. crawlo/tools/scenario_adapter.py +262 -262
  107. crawlo/utils/__init__.py +35 -35
  108. crawlo/utils/batch_processor.py +259 -259
  109. crawlo/utils/controlled_spider_mixin.py +439 -439
  110. crawlo/utils/date_tools.py +290 -290
  111. crawlo/utils/db_helper.py +343 -343
  112. crawlo/utils/enhanced_error_handler.py +356 -356
  113. crawlo/utils/env_config.py +105 -105
  114. crawlo/utils/error_handler.py +123 -123
  115. crawlo/utils/func_tools.py +82 -82
  116. crawlo/utils/large_scale_config.py +286 -286
  117. crawlo/utils/large_scale_helper.py +344 -344
  118. crawlo/utils/log.py +128 -128
  119. crawlo/utils/performance_monitor.py +285 -285
  120. crawlo/utils/queue_helper.py +175 -175
  121. crawlo/utils/redis_connection_pool.py +334 -334
  122. crawlo/utils/redis_key_validator.py +198 -198
  123. crawlo/utils/request.py +267 -267
  124. crawlo/utils/request_serializer.py +218 -218
  125. crawlo/utils/spider_loader.py +61 -61
  126. crawlo/utils/system.py +11 -11
  127. crawlo/utils/tools.py +4 -4
  128. crawlo/utils/url.py +39 -39
  129. {crawlo-1.2.3.dist-info → crawlo-1.2.4.dist-info}/METADATA +764 -692
  130. crawlo-1.2.4.dist-info/RECORD +206 -0
  131. examples/__init__.py +7 -7
  132. tests/DOUBLE_CRAWLO_PREFIX_FIX_REPORT.md +81 -81
  133. tests/__init__.py +7 -7
  134. tests/advanced_tools_example.py +275 -275
  135. tests/authenticated_proxy_example.py +236 -236
  136. tests/cleaners_example.py +160 -160
  137. tests/config_validation_demo.py +102 -102
  138. tests/controlled_spider_example.py +205 -205
  139. tests/date_tools_example.py +180 -180
  140. tests/dynamic_loading_example.py +523 -523
  141. tests/dynamic_loading_test.py +104 -104
  142. tests/env_config_example.py +133 -133
  143. tests/error_handling_example.py +171 -171
  144. tests/redis_key_validation_demo.py +130 -130
  145. tests/response_improvements_example.py +144 -144
  146. tests/test_advanced_tools.py +148 -148
  147. tests/test_all_redis_key_configs.py +145 -145
  148. tests/test_authenticated_proxy.py +141 -141
  149. tests/test_cleaners.py +54 -54
  150. tests/test_comprehensive.py +146 -146
  151. tests/test_config_validator.py +193 -193
  152. tests/test_crawlo_proxy_integration.py +172 -172
  153. tests/test_date_tools.py +123 -123
  154. tests/test_default_header_middleware.py +158 -158
  155. tests/test_double_crawlo_fix.py +207 -207
  156. tests/test_double_crawlo_fix_simple.py +124 -124
  157. tests/test_download_delay_middleware.py +221 -221
  158. tests/test_downloader_proxy_compatibility.py +268 -268
  159. tests/test_dynamic_downloaders_proxy.py +124 -124
  160. tests/test_dynamic_proxy.py +92 -92
  161. tests/test_dynamic_proxy_config.py +146 -146
  162. tests/test_dynamic_proxy_real.py +109 -109
  163. tests/test_edge_cases.py +303 -303
  164. tests/test_enhanced_error_handler.py +270 -270
  165. tests/test_env_config.py +121 -121
  166. tests/test_error_handler_compatibility.py +112 -112
  167. tests/test_final_validation.py +153 -153
  168. tests/test_framework_env_usage.py +103 -103
  169. tests/test_integration.py +356 -356
  170. tests/test_item_dedup_redis_key.py +122 -122
  171. tests/test_offsite_middleware.py +221 -221
  172. tests/test_parsel.py +29 -29
  173. tests/test_performance.py +327 -327
  174. tests/test_proxy_api.py +264 -264
  175. tests/test_proxy_health_check.py +32 -32
  176. tests/test_proxy_middleware.py +121 -121
  177. tests/test_proxy_middleware_enhanced.py +216 -216
  178. tests/test_proxy_middleware_integration.py +136 -136
  179. tests/test_proxy_providers.py +56 -56
  180. tests/test_proxy_stats.py +19 -19
  181. tests/test_proxy_strategies.py +59 -59
  182. tests/test_queue_manager_double_crawlo.py +173 -173
  183. tests/test_queue_manager_redis_key.py +176 -176
  184. tests/test_real_scenario_proxy.py +195 -195
  185. tests/test_redis_config.py +28 -28
  186. tests/test_redis_connection_pool.py +294 -294
  187. tests/test_redis_key_naming.py +181 -181
  188. tests/test_redis_key_validator.py +123 -123
  189. tests/test_redis_queue.py +224 -224
  190. tests/test_request_ignore_middleware.py +182 -182
  191. tests/test_request_serialization.py +70 -70
  192. tests/test_response_code_middleware.py +349 -349
  193. tests/test_response_filter_middleware.py +427 -427
  194. tests/test_response_improvements.py +152 -152
  195. tests/test_retry_middleware.py +241 -241
  196. tests/test_scheduler.py +241 -241
  197. tests/test_simple_response.py +61 -61
  198. tests/test_telecom_spider_redis_key.py +205 -205
  199. tests/test_template_content.py +87 -87
  200. tests/test_template_redis_key.py +134 -134
  201. tests/test_tools.py +153 -153
  202. tests/tools_example.py +257 -257
  203. crawlo-1.2.3.dist-info/RECORD +0 -222
  204. examples/aiohttp_settings.py +0 -42
  205. examples/curl_cffi_settings.py +0 -41
  206. examples/default_header_middleware_example.py +0 -107
  207. examples/default_header_spider_example.py +0 -129
  208. examples/download_delay_middleware_example.py +0 -160
  209. examples/httpx_settings.py +0 -42
  210. examples/multi_downloader_proxy_example.py +0 -81
  211. examples/offsite_middleware_example.py +0 -55
  212. examples/offsite_spider_example.py +0 -107
  213. examples/proxy_spider_example.py +0 -166
  214. examples/request_ignore_middleware_example.py +0 -51
  215. examples/request_ignore_spider_example.py +0 -99
  216. examples/response_code_middleware_example.py +0 -52
  217. examples/response_filter_middleware_example.py +0 -67
  218. examples/tong_hua_shun_settings.py +0 -62
  219. examples/tong_hua_shun_spider.py +0 -170
  220. {crawlo-1.2.3.dist-info → crawlo-1.2.4.dist-info}/WHEEL +0 -0
  221. {crawlo-1.2.3.dist-info → crawlo-1.2.4.dist-info}/entry_points.txt +0 -0
  222. {crawlo-1.2.3.dist-info → crawlo-1.2.4.dist-info}/top_level.txt +0 -0
@@ -1,692 +1,764 @@
1
- Metadata-Version: 2.4
2
- Name: crawlo
3
- Version: 1.2.3
4
- Summary: Crawlo 是一款基于异步IO的高性能Python爬虫框架,支持分布式抓取。
5
- Home-page: https://github.com/crawl-coder/Crawlo.git
6
- Author: crawl-coder
7
- Author-email: crawlo@qq.com
8
- License: MIT
9
- Classifier: Programming Language :: Python :: 3
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
12
- Requires-Python: >=3.6
13
- Description-Content-Type: text/markdown
14
- Requires-Dist: aiohttp>=3.12.14
15
- Requires-Dist: aiomysql>=0.2.0
16
- Requires-Dist: aioredis>=2.0.1
17
- Requires-Dist: asyncmy>=0.2.10
18
- Requires-Dist: cssselect>=1.2.0
19
- Requires-Dist: dateparser>=1.2.2
20
- Requires-Dist: httpx[http2]>=0.27.0
21
- Requires-Dist: curl-cffi>=0.13.0
22
- Requires-Dist: lxml>=5.2.1
23
- Requires-Dist: motor>=3.7.0
24
- Requires-Dist: parsel>=1.9.1
25
- Requires-Dist: pydantic>=2.11.7
26
- Requires-Dist: pymongo>=4.11
27
- Requires-Dist: PyMySQL>=1.1.1
28
- Requires-Dist: python-dateutil>=2.9.0.post0
29
- Requires-Dist: redis>=6.2.0
30
- Requires-Dist: requests>=2.32.4
31
- Requires-Dist: six>=1.17.0
32
- Requires-Dist: ujson>=5.9.0
33
- Requires-Dist: urllib3>=2.5.0
34
- Requires-Dist: w3lib>=2.1.2
35
- Requires-Dist: rich>=14.1.0
36
- Requires-Dist: astor>=0.8.1
37
- Requires-Dist: watchdog>=6.0.0
38
- Provides-Extra: render
39
- Requires-Dist: webdriver-manager>=4.0.0; extra == "render"
40
- Requires-Dist: playwright; extra == "render"
41
- Requires-Dist: selenium>=3.141.0; extra == "render"
42
- Provides-Extra: all
43
- Requires-Dist: bitarray>=1.5.3; extra == "all"
44
- Requires-Dist: PyExecJS>=1.5.1; extra == "all"
45
- Requires-Dist: pymongo>=3.10.1; extra == "all"
46
- Requires-Dist: redis-py-cluster>=2.1.0; extra == "all"
47
- Requires-Dist: webdriver-manager>=4.0.0; extra == "all"
48
- Requires-Dist: playwright; extra == "all"
49
- Requires-Dist: selenium>=3.141.0; extra == "all"
50
-
51
- <!-- markdownlint-disable MD033 MD041 -->
52
- <div align="center">
53
- <h1 align="center">Crawlo</h1>
54
- <p align="center">异步分布式爬虫框架</p>
55
- <p align="center"><strong>基于 asyncio 的高性能异步分布式爬虫框架,支持单机和分布式部署</strong></p>
56
-
57
- <p align="center">
58
- <a href="https://www.python.org/downloads/">
59
- <img src="https://img.shields.io/badge/python-3.8%2B-blue" alt="Python Version">
60
- </a>
61
- <a href="LICENSE">
62
- <img src="https://img.shields.io/badge/license-MIT-green" alt="License">
63
- </a>
64
- <a href="https://crawlo.readthedocs.io/">
65
- <img src="https://img.shields.io/badge/docs-latest-brightgreen" alt="Documentation">
66
- </a>
67
- <a href="https://github.com/crawlo/crawlo/actions">
68
- <img src="https://github.com/crawlo/crawlo/workflows/CI/badge.svg" alt="CI Status">
69
- </a>
70
- </p>
71
-
72
- <p align="center">
73
- <a href="#-特性">特性</a> •
74
- <a href="#-快速开始">快速开始</a> •
75
- <a href="#-命令行工具">命令行工具</a> •
76
- <a href="#-示例项目">示例项目</a>
77
- </p>
78
- </div>
79
-
80
- <br />
81
-
82
- <!-- 特性 section -->
83
- <div align="center">
84
- <h2>🌟 特性</h2>
85
-
86
- <table>
87
- <thead>
88
- <tr>
89
- <th>特性</th>
90
- <th>描述</th>
91
- </tr>
92
- </thead>
93
- <tbody>
94
- <tr>
95
- <td>⚡ <strong>异步高性能</strong></td>
96
- <td>基于 asyncio 实现,充分利用现代 CPU 夯性能</td>
97
- </tr>
98
- <tr>
99
- <td>🌐 <strong>分布式支持</strong></td>
100
- <td>内置 Redis 队列,轻松实现分布式部署</td>
101
- </tr>
102
- <tr>
103
- <td>🔧 <strong>模块化设计</strong></td>
104
- <td>中间件、管道、扩展组件系统,易于定制和扩展</td>
105
- </tr>
106
- <tr>
107
- <td>🔄 <strong>智能去重</strong></td>
108
- <td>多种去重策略(内存、Redis、Bloom Filter)</td>
109
- </tr>
110
- <tr>
111
- <td>⚙️ <strong>灵活配置</strong></td>
112
- <td>支持多种配置方式,适应不同场景需求</td>
113
- </tr>
114
- <tr>
115
- <td>📋 <strong>高级日志</strong></td>
116
- <td>支持日志轮转、结构化日志、JSON格式等高级功能</td>
117
- </tr>
118
- <tr>
119
- <td>📚 <strong>丰富文档</strong></td>
120
- <td>完整的中英文双语文档和示例项目</td>
121
- </tr>
122
- </tbody>
123
- </table>
124
- </div>
125
-
126
- <br />
127
-
128
- ---
129
-
130
- <!-- 快速开始 section -->
131
- <h2 align="center">🚀 快速开始</h2>
132
-
133
- ### 安装
134
-
135
- ```bash
136
- pip install crawlo
137
- ```
138
-
139
- ### 创建项目
140
-
141
- ```bash
142
- # 创建默认项目
143
- crawlo startproject myproject
144
-
145
- # 创建分布式模板项目
146
- crawlo startproject myproject distributed
147
-
148
- # 创建项目并选择特定模块
149
- crawlo startproject myproject --modules mysql,redis,proxy
150
-
151
- cd myproject
152
- ```
153
-
154
- ### 生成爬虫
155
-
156
- ```bash
157
- # 在项目目录中生成爬虫
158
- crawlo genspider news_spider news.example.com
159
- ```
160
-
161
- ### 编写爬虫
162
-
163
- ```python
164
- from crawlo import Spider, Request, Item
165
-
166
- class MyItem(Item):
167
- title = ''
168
- url = ''
169
-
170
- class MySpider(Spider):
171
- name = 'myspider'
172
-
173
- async def start_requests(self):
174
- yield Request('https://httpbin.org/get', callback=self.parse)
175
-
176
- async def parse(self, response):
177
- yield MyItem(
178
- title='Example Title',
179
- url=response.url
180
- )
181
- ```
182
-
183
- ### 运行爬虫
184
-
185
- ```bash
186
- # 使用命令行工具运行爬虫(推荐)
187
- crawlo run myspider
188
-
189
- # 使用项目自带的 run.py 脚本运行
190
- python run.py
191
-
192
- # 运行所有爬虫
193
- crawlo run all
194
-
195
- # 在项目子目录中也能正确运行
196
- cd subdirectory
197
- crawlo run myspider
198
- ```
199
-
200
- ---
201
-
202
- <!-- 命令行工具 section -->
203
- <h2 align="center">📜 命令行工具</h2>
204
-
205
- Crawlo 提供了丰富的命令行工具来帮助开发和管理爬虫项目:
206
-
207
- ### 获取帮助
208
-
209
- ```bash
210
- # 显示帮助信息
211
- crawlo -h
212
- crawlo --help
213
- crawlo help
214
- ```
215
-
216
- ### crawlo startproject
217
-
218
- 创建新的爬虫项目。
219
-
220
- ```bash
221
- # 基本用法
222
- crawlo startproject <project_name> [template_type] [--modules module1,module2]
223
-
224
- # 示例
225
- crawlo startproject my_spider_project
226
- crawlo startproject news_crawler simple
227
- crawlo startproject ecommerce_spider distributed --modules mysql,proxy
228
- ```
229
-
230
- **参数说明:**
231
- - `project_name`: 项目名称(必须是有效的Python标识符)
232
- - `template_type`: 模板类型(可选)
233
- - `default`: 默认模板 - 通用配置,适合大多数项目
234
- - `simple`: 简化模板 - 最小配置,适合快速开始
235
- - `distributed`: 分布式模板 - 针对分布式爬取优化
236
- - `high-performance`: 高性能模板 - 针对大规模高并发优化
237
- - `gentle`: 温和模板 - 低负载配置,对目标网站友好
238
- - `--modules`: 选择要包含的模块组件(可选)
239
- - `mysql`: MySQL数据库支持
240
- - `mongodb`: MongoDB数据库支持
241
- - `redis`: Redis支持(分布式队列和去重)
242
- - `proxy`: 代理支持
243
- - `monitoring`: 监控和性能分析
244
- - `dedup`: 去重功能
245
- - `httpx`: HttpX下载器
246
- - `aiohttp`: AioHttp下载器
247
- - `curl`: CurlCffi下载器
248
-
249
- ### crawlo genspider
250
-
251
- 在现有项目中生成新的爬虫。
252
-
253
- ```bash
254
- # 基本用法
255
- crawlo genspider <spider_name> <domain>
256
-
257
- # 示例
258
- crawlo genspider news_spider news.example.com
259
- crawlo genspider product_spider shop.example.com
260
- ```
261
-
262
- **参数说明:**
263
- - `spider_name`: 爬虫名称(必须是有效的Python标识符)
264
- - `domain`: 目标域名
265
-
266
- ### crawlo run
267
-
268
- 运行爬虫。
269
-
270
- ```bash
271
- # 基本用法
272
- crawlo run <spider_name>|all [--json] [--no-stats]
273
-
274
- # 示例
275
- crawlo run myspider
276
- crawlo run all
277
- crawlo run all --json --no-stats
278
- ```
279
-
280
- **参数说明:**
281
- - `spider_name`: 要运行的爬虫名称
282
- - `all`: 运行所有爬虫
283
- - `--json`: 以JSON格式输出结果
284
- - `--no-stats`: 不记录统计信息
285
-
286
- ### crawlo list
287
-
288
- 列出项目中所有可用的爬虫。
289
-
290
- ```bash
291
- # 基本用法
292
- crawlo list [--json]
293
-
294
- # 示例
295
- crawlo list
296
- crawlo list --json
297
- ```
298
-
299
- **参数说明:**
300
- - `--json`: 以JSON格式输出结果
301
-
302
- ### crawlo check
303
-
304
- 检查爬虫定义的合规性。
305
-
306
- ```bash
307
- # 基本用法
308
- crawlo check [--fix] [--ci] [--json] [--watch]
309
-
310
- # 示例
311
- crawlo check
312
- crawlo check --fix
313
- crawlo check --ci
314
- crawlo check --watch
315
- ```
316
-
317
- **参数说明:**
318
- - `--fix`: 自动修复常见问题
319
- - `--ci`: CI模式输出(简洁格式)
320
- - `--json`: 以JSON格式输出结果
321
- - `--watch`: 监听模式,文件更改时自动检查
322
-
323
- ### crawlo stats
324
-
325
- 查看爬虫运行统计信息。
326
-
327
- ```bash
328
- # 基本用法
329
- crawlo stats [spider_name] [--all]
330
-
331
- # 示例
332
- crawlo stats
333
- crawlo stats myspider
334
- crawlo stats myspider --all
335
- ```
336
-
337
- **参数说明:**
338
- - `spider_name`: 指定要查看统计信息的爬虫名称
339
- - `--all`: 显示指定爬虫的所有历史运行记录
340
-
341
- ---
342
-
343
- <!-- 架构设计 section -->
344
- <h2 align="center">🏗️ 架构设计</h2>
345
-
346
- ### 核心组件说明
347
-
348
- Crawlo 框架由以下核心组件构成:
349
-
350
- <table>
351
- <thead>
352
- <tr>
353
- <th>组件</th>
354
- <th>功能描述</th>
355
- </tr>
356
- </thead>
357
- <tbody>
358
- <tr>
359
- <td><strong>Crawler</strong></td>
360
- <td>爬虫运行实例,管理Spider与引擎的生命周期</td>
361
- </tr>
362
- <tr>
363
- <td><strong>Engine</strong></td>
364
- <td>引擎组件,协调Scheduler、Downloader、Processor</td>
365
- </tr>
366
- <tr>
367
- <td><strong>Scheduler</strong></td>
368
- <td>调度器,管理请求队列和去重过滤</td>
369
- </tr>
370
- <tr>
371
- <td><strong>Downloader</strong></td>
372
- <td>下载器,负责网络请求,支持多种实现(aiohttp, httpx, curl-cffi)</td>
373
- </tr>
374
- <tr>
375
- <td><strong>Processor</strong></td>
376
- <td>处理器,处理响应数据和管道</td>
377
- </tr>
378
- <tr>
379
- <td><strong>QueueManager</strong></td>
380
- <td>统一的队列管理器,支持内存队列和Redis队列的自动切换</td>
381
- </tr>
382
- <tr>
383
- <td><strong>Filter</strong></td>
384
- <td>请求去重过滤器,支持内存和Redis两种实现</td>
385
- </tr>
386
- <tr>
387
- <td><strong>Middleware</strong></td>
388
- <td>中间件系统,处理请求/响应的预处理和后处理</td>
389
- </tr>
390
- <tr>
391
- <td><strong>Pipeline</strong></td>
392
- <td>数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能</td>
393
- </tr>
394
- <tr>
395
- <td><strong>Spider</strong></td>
396
- <td>爬虫基类,定义爬取逻辑</td>
397
- </tr>
398
- </tbody>
399
- </table>
400
-
401
- ### 运行模式
402
-
403
- Crawlo支持三种运行模式:
404
-
405
- <table>
406
- <thead>
407
- <tr>
408
- <th>模式</th>
409
- <th>描述</th>
410
- <th>队列类型</th>
411
- <th>过滤器类型</th>
412
- </tr>
413
- </thead>
414
- <tbody>
415
- <tr>
416
- <td><strong>standalone</strong></td>
417
- <td>单机模式</td>
418
- <td>内存队列</td>
419
- <td>内存过滤器</td>
420
- </tr>
421
- <tr>
422
- <td><strong>distributed</strong></td>
423
- <td>分布式模式</td>
424
- <td>Redis队列</td>
425
- <td>Redis过滤器</td>
426
- </tr>
427
- <tr>
428
- <td><strong>auto</strong></td>
429
- <td>自动检测模式</td>
430
- <td>根据环境自动选择最佳运行方式</td>
431
- <td>根据环境自动选择</td>
432
- </tr>
433
- </tbody>
434
- </table>
435
-
436
- ### 模块层次结构
437
-
438
- ```
439
- crawlo/
440
- ├── cli.py # 命令行接口
441
- ├── crawler.py # 爬虫运行实例
442
- ├── project.py # 项目管理
443
- ├── config.py # 配置管理
444
- ├── mode_manager.py # 运行模式管理器
445
- ├── stats_collector.py # 统计收集器
446
- ├── subscriber.py # 事件订阅器
447
- ├── task_manager.py # 任务管理器
448
- ├── event.py # 事件定义
449
- ├── exceptions.py # 异常定义
450
- ├──
451
- ├── core/ # 核心组件
452
- │ ├── engine.py # 引擎
453
- │ ├── scheduler.py # 调度器
454
- │ ├── processor.py # 处理器
455
-
456
- ├── spider/ # 爬虫基类
457
- │ └── __init__.py # 爬虫元类和基类
458
-
459
- ├── network/ # 网络相关
460
- │ ├── request.py # 请求对象
461
- │ └── response.py # 响应对象
462
-
463
- ├── downloader/ # 下载器
464
- │ ├── __init__.py # 下载器基类
465
- │ ├── aiohttp_downloader.py # AioHttp实现
466
- │ ├── httpx_downloader.py # HttpX实现
467
- │ └── cffi_downloader.py # CurlCffi实现
468
-
469
- ├── queue/ # 队列管理
470
- │ ├── __init__.py
471
- │ ├── queue_manager.py # 队列管理器
472
- │ ├── pqueue.py # 内存优先队列
473
- │ └── redis_priority_queue.py # Redis优先队列
474
-
475
- ├── filters/ # 过滤器
476
- │ ├── __init__.py
477
- │ ├── base_filter.py # 过滤器基类
478
- │ ├── memory_filter.py # 内存过滤器
479
- │ └── aioredis_filter.py # Redis过滤器
480
-
481
- ├── middleware/ # 中间件
482
- │ ├── __init__.py
483
- │ ├── middleware_manager.py # 中间件管理器
484
- │ ├── default_header.py # 默认请求头
485
- │ ├── download_delay.py # 下载延迟
486
- │ ├── proxy.py # 代理支持
487
- │ ├── request_ignore.py # 请求忽略
488
- │ ├── response_code.py # 响应码处理
489
- │ ├── response_filter.py # 响应过滤
490
- │ └── retry.py # 重试机制
491
-
492
- ├── pipelines/ # 数据管道
493
- │ ├── __init__.py
494
- │ ├── pipeline_manager.py # 管道管理器
495
- │ ├── base_pipeline.py # 管道基类
496
- │ ├── console_pipeline.py # 控制台输出管道
497
- │ ├── json_pipeline.py # JSON存储管道
498
- │ ├── redis_dedup_pipeline.py # Redis去重管道
499
- │ └── mysql_pipeline.py # MySQL存储管道
500
-
501
- ├── extension/ # 扩展组件
502
- │ ├── __init__.py
503
- │ ├── log_interval.py # 定时日志
504
- │ ├── log_stats.py # 统计日志
505
- │ ├── logging_extension.py # 日志扩展
506
- │ ├── memory_monitor.py # 内存监控
507
- │ ├── performance_profiler.py # 性能分析
508
- │ ├── health_check.py # 健康检查
509
- │ └── request_recorder.py # 请求记录
510
-
511
- ├── settings/ # 配置系统
512
- │ ├── __init__.py
513
- │ ├── default_settings.py # 默认配置
514
- │ └── setting_manager.py # 配置管理器
515
-
516
- ├── utils/ # 工具库
517
- │ ├── __init__.py
518
- │ ├── log.py # 基础日志工具
519
- │ ├── request.py # 请求工具
520
- │ ├── request_serializer.py # 请求序列化
521
- │ └── func_tools.py # 函数工具
522
-
523
- └── templates/ # 模板文件
524
- ├── project/
525
- └── spider/
526
- ```
527
-
528
- ---
529
-
530
- <!-- 配置系统 section -->
531
- <h2 align="center">🎛️ 配置系统</h2>
532
-
533
- ### 传统配置方式
534
-
535
- ```
536
- # settings.py
537
- PROJECT_NAME = 'myproject'
538
- CONCURRENCY = 16
539
- DOWNLOAD_DELAY = 1.0
540
- QUEUE_TYPE = 'memory' # 单机模式
541
- # QUEUE_TYPE = 'redis' # 分布式模式
542
-
543
- # Redis 配置 (分布式模式下使用)
544
- REDIS_HOST = 'localhost'
545
- REDIS_PORT = 6379
546
- REDIS_DB = 0
547
- REDIS_PASSWORD = ''
548
-
549
- # 数据管道配置
550
- PIPELINES = [
551
- 'crawlo.pipelines.console_pipeline.ConsolePipeline',
552
- 'crawlo.pipelines.json_pipeline.JsonPipeline',
553
- 'crawlo.pipelines.redis_dedup_pipeline.RedisDedupPipeline', # Redis去重管道
554
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL存储管道
555
- ]
556
-
557
- # 高级日志配置
558
- LOG_FILE = 'logs/spider.log'
559
- LOG_LEVEL = 'INFO'
560
- LOG_MAX_BYTES = 10 * 1024 * 1024 # 10MB
561
- LOG_BACKUP_COUNT = 5
562
- LOG_JSON_FORMAT = False # 设置为True启用JSON格式
563
-
564
- # 启用高级日志扩展
565
- ADVANCED_LOGGING_ENABLED = True
566
-
567
- # 启用日志监控
568
- LOG_MONITOR_ENABLED = True
569
- LOG_MONITOR_INTERVAL = 30
570
- LOG_MONITOR_DETAILED_STATS = True
571
-
572
- # 添加扩展
573
- EXTENSIONS = [
574
- 'crawlo.extension.log_interval.LogIntervalExtension',
575
- 'crawlo.extension.log_stats.LogStats',
576
- 'crawlo.extension.logging_extension.CustomLoggerExtension',
577
- 'crawlo.extension.memory_monitor.MemoryMonitorExtension',
578
- ]
579
- ```
580
-
581
- ### MySQL 管道配置
582
-
583
- Crawlo 提供了现成的 MySQL 管道实现,可以轻松将爬取的数据存储到 MySQL 数据库中:
584
-
585
- ```
586
- # 在 settings.py 中启用 MySQL 管道
587
- PIPELINES = [
588
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline',
589
- ]
590
-
591
- # MySQL 数据库配置
592
- MYSQL_HOST = 'localhost'
593
- MYSQL_PORT = 3306
594
- MYSQL_USER = 'your_username'
595
- MYSQL_PASSWORD = 'your_password'
596
- MYSQL_DB = 'your_database'
597
- MYSQL_TABLE = 'your_table_name'
598
-
599
- # 可选的批量插入配置
600
- MYSQL_BATCH_SIZE = 100
601
- MYSQL_USE_BATCH = True
602
- ```
603
-
604
- MySQL 管道特性:
605
- - **异步操作**:基于 asyncmy 驱动,提供高性能的异步数据库操作
606
- - **连接池**:自动管理数据库连接,提高效率
607
- - **批量插入**:支持批量插入以提高性能
608
- - **事务支持**:确保数据一致性
609
- - **灵活配置**:支持自定义表名、批量大小等参数
610
-
611
- ### 命令行配置
612
-
613
- ```
614
- # 运行单个爬虫
615
- crawlo run myspider
616
-
617
- # 运行所有爬虫
618
- crawlo run all
619
-
620
- # 在项目子目录中也能正确运行
621
- cd subdirectory
622
- crawlo run myspider
623
- ```
624
-
625
- ---
626
-
627
- <!-- 核心组件 section -->
628
- <h2 align="center">🧩 核心组件</h2>
629
-
630
- ### 中间件系统
631
- 灵活的中间件系统,支持请求预处理、响应处理和异常处理。
632
-
633
- ### 管道系统
634
- 可扩展的数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能:
635
- - **ConsolePipeline**: 控制台输出管道
636
- - **JsonPipeline**: JSON文件存储管道
637
- - **RedisDedupPipeline**: Redis去重管道,基于Redis集合实现分布式去重
638
- - **AsyncmyMySQLPipeline**: MySQL数据库存储管道,基于asyncmy驱动
639
-
640
- ### 扩展组件
641
- 功能增强扩展,包括日志、监控、性能分析等:
642
- - **LogIntervalExtension**: 定时日志扩展
643
- - **LogStats**: 统计日志扩展
644
- - **CustomLoggerExtension**: 自定义日志扩展
645
- - **MemoryMonitorExtension**: 内存监控扩展
646
- - **PerformanceProfilerExtension**: 性能分析扩展
647
- - **HealthCheckExtension**: 健康检查扩展
648
- - **RequestRecorderExtension**: 请求记录扩展
649
-
650
- ### 过滤系统
651
- 智能去重过滤,支持多种去重策略(内存、Redis、Bloom Filter)。
652
-
653
- ---
654
-
655
- <!-- 示例项目 section -->
656
- <h2 align="center">📦 示例项目</h2>
657
-
658
- - [OFweek分布式爬虫](examples/ofweek_distributed/) - 复杂的分布式爬虫示例,包含Redis去重功能
659
- - [OFweek独立爬虫](examples/ofweek_standalone/) - 独立运行的爬虫示例
660
- - [OFweek混合模式爬虫](examples/ofweek_spider/) - 支持单机和分布式模式切换的爬虫示例
661
-
662
- ---
663
-
664
- <!-- 文档 section -->
665
- <h2 align="center">📚 文档</h2>
666
-
667
- 完整的文档请访问 [Crawlo Documentation](https://crawlo.readthedocs.io/)
668
-
669
- - [快速开始指南](docs/modules/index.md)
670
- - [模块化文档](docs/modules/index.md)
671
- - [核心引擎文档](docs/modules/core/engine.md)
672
- - [调度器文档](docs/modules/core/scheduler.md)
673
- - [下载器文档](docs/modules/downloader/index.md)
674
- - [中间件文档](docs/modules/middleware/index.md)
675
- - [管道文档](docs/modules/pipeline/index.md)
676
- - [队列文档](docs/modules/queue/index.md)
677
- - [过滤器文档](docs/modules/filter/index.md)
678
- - [扩展组件文档](docs/modules/extension/index.md)
679
-
680
- ---
681
-
682
- <!-- 贡献 section -->
683
- <h2 align="center">🤝 贡献</h2>
684
-
685
- 欢迎提交 Issue 和 Pull Request 来帮助改进 Crawlo!
686
-
687
- ---
688
-
689
- <!-- 许可证 section -->
690
- <h2 align="center">📄 许可证</h2>
691
-
692
- 本项目采用 MIT 许可证,详情请见 [LICENSE](LICENSE) 文件。
1
+ Metadata-Version: 2.4
2
+ Name: crawlo
3
+ Version: 1.2.4
4
+ Summary: Crawlo 是一款基于异步IO的高性能Python爬虫框架,支持分布式抓取。
5
+ Home-page: https://github.com/crawl-coder/Crawlo.git
6
+ Author: crawl-coder
7
+ Author-email: crawlo@qq.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.6
13
+ Description-Content-Type: text/markdown
14
+ Requires-Dist: aiohttp>=3.12.14
15
+ Requires-Dist: aiomysql>=0.2.0
16
+ Requires-Dist: aioredis>=2.0.1
17
+ Requires-Dist: asyncmy>=0.2.10
18
+ Requires-Dist: cssselect>=1.2.0
19
+ Requires-Dist: dateparser>=1.2.2
20
+ Requires-Dist: httpx[http2]>=0.27.0
21
+ Requires-Dist: curl-cffi>=0.13.0
22
+ Requires-Dist: lxml>=5.2.1
23
+ Requires-Dist: motor>=3.7.0
24
+ Requires-Dist: parsel>=1.9.1
25
+ Requires-Dist: pydantic>=2.11.7
26
+ Requires-Dist: pymongo>=4.11
27
+ Requires-Dist: PyMySQL>=1.1.1
28
+ Requires-Dist: python-dateutil>=2.9.0.post0
29
+ Requires-Dist: redis>=6.2.0
30
+ Requires-Dist: requests>=2.32.4
31
+ Requires-Dist: six>=1.17.0
32
+ Requires-Dist: ujson>=5.9.0
33
+ Requires-Dist: urllib3>=2.5.0
34
+ Requires-Dist: w3lib>=2.1.2
35
+ Requires-Dist: rich>=14.1.0
36
+ Requires-Dist: astor>=0.8.1
37
+ Requires-Dist: watchdog>=6.0.0
38
+ Provides-Extra: render
39
+ Requires-Dist: webdriver-manager>=4.0.0; extra == "render"
40
+ Requires-Dist: playwright; extra == "render"
41
+ Requires-Dist: selenium>=3.141.0; extra == "render"
42
+ Provides-Extra: all
43
+ Requires-Dist: bitarray>=1.5.3; extra == "all"
44
+ Requires-Dist: PyExecJS>=1.5.1; extra == "all"
45
+ Requires-Dist: pymongo>=3.10.1; extra == "all"
46
+ Requires-Dist: redis-py-cluster>=2.1.0; extra == "all"
47
+ Requires-Dist: webdriver-manager>=4.0.0; extra == "all"
48
+ Requires-Dist: playwright; extra == "all"
49
+ Requires-Dist: selenium>=3.141.0; extra == "all"
50
+
51
+ <!-- markdownlint-disable MD033 MD041 -->
52
+ <div align="center">
53
+ <h1 align="center">Crawlo</h1>
54
+ <p align="center">异步分布式爬虫框架</p>
55
+ <p align="center"><strong>基于 asyncio 的高性能异步分布式爬虫框架,支持单机和分布式部署</strong></p>
56
+
57
+ <p align="center">
58
+ <a href="https://www.python.org/downloads/">
59
+ <img src="https://img.shields.io/badge/python-3.8%2B-blue" alt="Python Version">
60
+ </a>
61
+ <a href="LICENSE">
62
+ <img src="https://img.shields.io/badge/license-MIT-green" alt="License">
63
+ </a>
64
+ <a href="https://crawlo.readthedocs.io/">
65
+ <img src="https://img.shields.io/badge/docs-latest-brightgreen" alt="Documentation">
66
+ </a>
67
+ <a href="https://github.com/crawlo/crawlo/actions">
68
+ <img src="https://github.com/crawlo/crawlo/workflows/CI/badge.svg" alt="CI Status">
69
+ </a>
70
+ </p>
71
+
72
+ <p align="center">
73
+ <a href="#-特性">特性</a> •
74
+ <a href="#-快速开始">快速开始</a> •
75
+ <a href="#-命令行工具">命令行工具</a> •
76
+ <a href="#-示例项目">示例项目</a>
77
+ </p>
78
+ </div>
79
+
80
+ <br />
81
+
82
+ <!-- 特性 section -->
83
+ <div align="center">
84
+ <h2>🌟 特性</h2>
85
+
86
+ <table>
87
+ <thead>
88
+ <tr>
89
+ <th>特性</th>
90
+ <th>描述</th>
91
+ </tr>
92
+ </thead>
93
+ <tbody>
94
+ <tr>
95
+ <td>⚡ <strong>异步高性能</strong></td>
96
+ <td>基于 asyncio 实现,充分利用现代 CPU 夯性能</td>
97
+ </tr>
98
+ <tr>
99
+ <td>🌐 <strong>分布式支持</strong></td>
100
+ <td>内置 Redis 队列,轻松实现分布式部署</td>
101
+ </tr>
102
+ <tr>
103
+ <td>🔧 <strong>模块化设计</strong></td>
104
+ <td>中间件、管道、扩展组件系统,易于定制和扩展</td>
105
+ </tr>
106
+ <tr>
107
+ <td>🔄 <strong>智能去重</strong></td>
108
+ <td>多种去重策略(内存、Redis、Bloom Filter)</td>
109
+ </tr>
110
+ <tr>
111
+ <td>⚙️ <strong>灵活配置</strong></td>
112
+ <td>支持多种配置方式,适应不同场景需求</td>
113
+ </tr>
114
+ <tr>
115
+ <td>📋 <strong>高级日志</strong></td>
116
+ <td>支持日志轮转、结构化日志、JSON格式等高级功能</td>
117
+ </tr>
118
+ <tr>
119
+ <td>📚 <strong>丰富文档</strong></td>
120
+ <td>完整的中英文双语文档和示例项目</td>
121
+ </tr>
122
+ </tbody>
123
+ </table>
124
+ </div>
125
+
126
+ <br />
127
+
128
+ ---
129
+
130
+ <!-- 快速开始 section -->
131
+ <h2 align="center">🚀 快速开始</h2>
132
+
133
+ ### 安装
134
+
135
+ ```bash
136
+ pip install crawlo
137
+ ```
138
+
139
+ ### 创建项目
140
+
141
+ ```bash
142
+ # 创建默认项目
143
+ crawlo startproject myproject
144
+
145
+ # 创建分布式模板项目
146
+ crawlo startproject myproject distributed
147
+
148
+ # 创建项目并选择特定模块
149
+ crawlo startproject myproject --modules mysql,redis,proxy
150
+
151
+ cd myproject
152
+ ```
153
+
154
+ ### 生成爬虫
155
+
156
+ ```bash
157
+ # 在项目目录中生成爬虫
158
+ crawlo genspider news_spider news.example.com
159
+ ```
160
+
161
+ ### 编写爬虫
162
+
163
+ ```python
164
+ from crawlo import Spider, Request, Item
165
+
166
+ class MyItem(Item):
167
+ title = ''
168
+ url = ''
169
+
170
+ class MySpider(Spider):
171
+ name = 'myspider'
172
+
173
+ async def start_requests(self):
174
+ yield Request('https://httpbin.org/get', callback=self.parse)
175
+
176
+ async def parse(self, response):
177
+ yield MyItem(
178
+ title='Example Title',
179
+ url=response.url
180
+ )
181
+ ```
182
+
183
+ ### 运行爬虫
184
+
185
+ ```bash
186
+ # 使用命令行工具运行爬虫(推荐)
187
+ crawlo run myspider
188
+
189
+ # 使用项目自带的 run.py 脚本运行
190
+ python run.py
191
+
192
+ # 运行所有爬虫
193
+ crawlo run all
194
+
195
+ # 在项目子目录中也能正确运行
196
+ cd subdirectory
197
+ crawlo run myspider
198
+ ```
199
+
200
+ ---
201
+
202
+ <!-- 命令行工具 section -->
203
+ <h2 align="center">🔧 命令行工具</h2>
204
+
205
+ Crawlo 提供了丰富的命令行工具,简化项目创建和管理。
206
+
207
+ ### crawlo startproject
208
+
209
+ 创建新的爬虫项目。
210
+
211
+ ```bash
212
+ # 创建默认项目
213
+ crawlo startproject myproject
214
+
215
+ # 创建指定模板的项目
216
+ crawlo startproject myproject simple
217
+ crawlo startproject myproject distributed
218
+ ```
219
+
220
+ ### crawlo genspider
221
+
222
+ 在现有项目中生成新的爬虫。
223
+
224
+ ```bash
225
+ # 在当前目录生成爬虫
226
+ crawlo genspider myspider http://example.com
227
+
228
+ # 指定模板生成爬虫
229
+ crawlo genspider myspider http://example.com --template basic
230
+ ```
231
+
232
+ ### crawlo run
233
+
234
+ 运行指定的爬虫。
235
+
236
+ ```bash
237
+ # 运行单个爬虫
238
+ crawlo run myspider
239
+
240
+ # 运行所有爬虫
241
+ crawlo run all
242
+
243
+ # 以JSON格式输出结果
244
+ crawlo run myspider --json
245
+
246
+ # 禁用统计信息
247
+ crawlo run myspider --no-stats
248
+ ```
249
+
250
+ ### crawlo list
251
+
252
+ 列出项目中所有可用的爬虫。
253
+
254
+ ```bash
255
+ crawlo list
256
+ ```
257
+
258
+ ### crawlo check
259
+
260
+ 检查项目配置和爬虫实现。
261
+
262
+ ```bash
263
+ crawlo check
264
+ ```
265
+
266
+ ### crawlo stats
267
+
268
+ 查看爬虫统计数据。
269
+
270
+ ```bash
271
+ # 查看最新统计数据
272
+ crawlo stats
273
+
274
+ # 查看指定爬虫的统计数据
275
+ crawlo stats myspider
276
+ ```
277
+
278
+ ---
279
+
280
+ ### 分布式模式
281
+
282
+ Crawlo支持分布式爬取,通过Redis实现任务队列和去重过滤,支持多节点协同工作。
283
+
284
+ #### 配置分布式项目
285
+
286
+ ```bash
287
+ # 创建分布式模板项目
288
+ crawlo startproject myproject distributed
289
+
290
+ cd myproject
291
+ ```
292
+
293
+ #### 运行分布式爬虫
294
+
295
+ ```
296
+ # 使用命令行工具运行分布式爬虫
297
+ crawlo run-distributed myspider
298
+
299
+ # 指定Redis配置运行
300
+ crawlo run-distributed myspider --redis-host 192.168.1.100 --redis-port 6379
301
+
302
+ # 使用项目自带的 run_distributed.py 脚本运行
303
+ python run_distributed.py --spider myspider
304
+
305
+ # 使用 crawlo run 命令运行分布式模式
306
+ crawlo run myspider
307
+ ```
308
+
309
+ #### 分布式配置
310
+
311
+ 分布式模式使用Redis作为任务队列和去重过滤器:
312
+
313
+ - **队列后端**: Redis(RedisPriorityQueue)
314
+ - **去重过滤器**: AioRedisFilter
315
+ - **状态共享**: Redis协调
316
+ - **可扩展性**: 多节点集群
317
+
318
+ <!-- 配置方式 section -->
319
+ <h2 align="center">⚙️ 配置方式</h2>
320
+
321
+ Crawlo 提供了多种灵活的配置方式,以适应不同的使用场景和开发需求。
322
+
323
+ ### 三种配置方式详解
324
+
325
+ #### 1. 配置工厂方式(推荐)
326
+
327
+ 使用 `CrawloConfig` 配置工厂是推荐的配置方式,它提供了类型安全和智能提示。
328
+
329
+ ```python
330
+ from crawlo.config import CrawloConfig
331
+ from crawlo.crawler import CrawlerProcess
332
+
333
+ # 单机模式配置
334
+ config = CrawloConfig.standalone(
335
+ concurrency=8,
336
+ download_delay=1.0
337
+ )
338
+
339
+ # 分布式模式配置
340
+ config = CrawloConfig.distributed(
341
+ redis_host='127.0.0.1',
342
+ redis_port=6379,
343
+ project_name='myproject',
344
+ concurrency=16
345
+ )
346
+
347
+ # 自动检测模式配置
348
+ config = CrawloConfig.auto(concurrency=12)
349
+
350
+ # 从环境变量读取配置
351
+ config = CrawloConfig.from_env()
352
+
353
+ # 创建爬虫进程
354
+ process = CrawlerProcess(settings=config.to_dict())
355
+ ```
356
+
357
+ #### 2. 直接配置方式
358
+
359
+ 直接在 `settings.py` 文件中配置各项参数,适合需要精细控制的场景。
360
+
361
+ ```python
362
+ # settings.py
363
+ PROJECT_NAME = 'myproject'
364
+ RUN_MODE = 'standalone' # 或 'distributed' 或 'auto'
365
+ CONCURRENCY = 8
366
+ DOWNLOAD_DELAY = 1.0
367
+
368
+ # 分布式模式下需要配置Redis
369
+ REDIS_HOST = '127.0.0.1'
370
+ REDIS_PORT = 6379
371
+ REDIS_PASSWORD = ''
372
+
373
+ # 其他配置...
374
+ ```
375
+
376
+ #### 3. 环境变量方式
377
+
378
+ 通过环境变量配置,适合部署和CI/CD场景。
379
+
380
+ ```bash
381
+ # 设置环境变量
382
+ export CRAWLO_MODE=standalone
383
+ export CONCURRENCY=8
384
+ export DOWNLOAD_DELAY=1.0
385
+ export REDIS_HOST=127.0.0.1
386
+ export REDIS_PORT=6379
387
+ ```
388
+
389
+ ```python
390
+ # 在代码中读取环境变量
391
+ from crawlo.config import CrawloConfig
392
+ config = CrawloConfig.from_env()
393
+ process = CrawlerProcess(settings=config.to_dict())
394
+ ```
395
+
396
+ ### 不同运行模式下的最佳配置方式
397
+
398
+ #### 单机模式 (standalone)
399
+
400
+ 适用于开发调试、小规模数据采集、个人项目。
401
+
402
+ **推荐配置方式:**
403
+ ```python
404
+ from crawlo.config import CrawloConfig
405
+ config = CrawloConfig.standalone(concurrency=4, download_delay=1.0)
406
+ process = CrawlerProcess(settings=config.to_dict())
407
+ ```
408
+
409
+ **特点:**
410
+ - 简单易用,资源占用少
411
+ - 无需额外依赖(如Redis)
412
+ - 适合个人开发环境
413
+
414
+ #### 分布式模式 (distributed)
415
+
416
+ 适用于大规模数据采集、多节点协同工作、高并发需求。
417
+
418
+ **推荐配置方式:**
419
+ ```python
420
+ from crawlo.config import CrawloConfig
421
+ config = CrawloConfig.distributed(
422
+ redis_host='your_redis_host',
423
+ redis_port=6379,
424
+ project_name='myproject',
425
+ concurrency=16
426
+ )
427
+ process = CrawlerProcess(settings=config.to_dict())
428
+ ```
429
+
430
+ **特点:**
431
+ - 支持多节点扩展
432
+ - 高并发处理能力
433
+ - 需要Redis支持
434
+
435
+ #### 自动检测模式 (auto)
436
+
437
+ 适用于希望根据环境自动选择最佳运行方式。
438
+
439
+ **推荐配置方式:**
440
+ ```python
441
+ from crawlo.config import CrawloConfig
442
+ config = CrawloConfig.auto(concurrency=12)
443
+ process = CrawlerProcess(settings=config.to_dict())
444
+ ```
445
+
446
+ **特点:**
447
+ - 智能检测环境配置
448
+ - 自动选择运行模式
449
+ - 适合在不同环境中使用同一套配置
450
+
451
+ <!-- 架构设计 section -->
452
+ <h2 align="center">🏗️ 架构设计</h2>
453
+
454
+ ### 核心组件说明
455
+
456
+ Crawlo 框架由以下核心组件构成:
457
+
458
+ <table>
459
+ <thead>
460
+ <tr>
461
+ <th>组件</th>
462
+ <th>功能描述</th>
463
+ </tr>
464
+ </thead>
465
+ <tbody>
466
+ <tr>
467
+ <td><strong>Crawler</strong></td>
468
+ <td>爬虫运行实例,管理Spider与引擎的生命周期</td>
469
+ </tr>
470
+ <tr>
471
+ <td><strong>Engine</strong></td>
472
+ <td>引擎组件,协调Scheduler、Downloader、Processor</td>
473
+ </tr>
474
+ <tr>
475
+ <td><strong>Scheduler</strong></td>
476
+ <td>调度器,管理请求队列和去重过滤</td>
477
+ </tr>
478
+ <tr>
479
+ <td><strong>Downloader</strong></td>
480
+ <td>下载器,负责网络请求,支持多种实现(aiohttp, httpx, curl-cffi)</td>
481
+ </tr>
482
+ <tr>
483
+ <td><strong>Processor</strong></td>
484
+ <td>处理器,处理响应数据和管道</td>
485
+ </tr>
486
+ <tr>
487
+ <td><strong>QueueManager</strong></td>
488
+ <td>统一的队列管理器,支持内存队列和Redis队列的自动切换</td>
489
+ </tr>
490
+ <tr>
491
+ <td><strong>Filter</strong></td>
492
+ <td>请求去重过滤器,支持内存和Redis两种实现</td>
493
+ </tr>
494
+ <tr>
495
+ <td><strong>Middleware</strong></td>
496
+ <td>中间件系统,处理请求/响应的预处理和后处理</td>
497
+ </tr>
498
+ <tr>
499
+ <td><strong>Pipeline</strong></td>
500
+ <td>数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能</td>
501
+ </tr>
502
+ <tr>
503
+ <td><strong>Spider</strong></td>
504
+ <td>爬虫基类,定义爬取逻辑</td>
505
+ </tr>
506
+ </tbody>
507
+ </table>
508
+
509
+ ### 运行模式
510
+
511
+ Crawlo支持三种运行模式:
512
+
513
+ <table>
514
+ <thead>
515
+ <tr>
516
+ <th>模式</th>
517
+ <th>描述</th>
518
+ <th>队列类型</th>
519
+ <th>过滤器类型</th>
520
+ </tr>
521
+ </thead>
522
+ <tbody>
523
+ <tr>
524
+ <td><strong>standalone</strong></td>
525
+ <td>单机模式</td>
526
+ <td>内存队列</td>
527
+ <td>内存过滤器</td>
528
+ </tr>
529
+ <tr>
530
+ <td><strong>distributed</strong></td>
531
+ <td>分布式模式</td>
532
+ <td>Redis队列</td>
533
+ <td>Redis过滤器</td>
534
+ </tr>
535
+ <tr>
536
+ <td><strong>auto</strong></td>
537
+ <td>自动检测模式</td>
538
+ <td>根据环境自动选择最佳运行方式</td>
539
+ <td>根据环境自动选择</td>
540
+ </tr>
541
+ </tbody>
542
+ </table>
543
+
544
+ #### 运行模式选择指南
545
+
546
+ ##### 1. 单机模式 (standalone)
547
+ - **适用场景**:
548
+ - 开发和测试阶段
549
+ - 小规模数据采集(几千到几万条数据)
550
+ - 学习和演示用途
551
+ - 对目标网站负载要求不高的场景
552
+ - **优势**:
553
+ - 配置简单,无需额外依赖
554
+ - 资源消耗低
555
+ - 启动快速
556
+ - 适合本地开发调试
557
+ - **限制**:
558
+ - 无法跨会话去重
559
+ - 无法分布式部署
560
+ - 内存占用随数据量增长
561
+
562
+ ##### 2. 分布式模式 (distributed)
563
+ - **适用场景**:
564
+ - 大规模数据采集(百万级以上)
565
+ - 需要多节点协同工作
566
+ - 要求跨会话、跨节点去重
567
+ - 生产环境部署
568
+ - **优势**:
569
+ - 支持水平扩展
570
+ - 跨节点任务协调
571
+ - 持久化去重过滤
572
+ - 高可用性
573
+ - **要求**:
574
+ - 需要Redis服务器
575
+ - 网络环境稳定
576
+ - 更复杂的配置管理
577
+
578
+ ##### 3. 自动模式 (auto)
579
+ - **适用场景**:
580
+ - 希望根据环境自动选择最佳配置
581
+ - 开发和生产环境使用同一套代码
582
+ - 动态适应运行环境
583
+ - **工作机制**:
584
+ - 检测Redis可用性
585
+ - Redis可用时自动切换到分布式模式
586
+ - Redis不可用时回退到单机模式
587
+ - **优势**:
588
+ - 环境适应性强
589
+ - 部署灵活
590
+ - 开发和生产环境配置统一
591
+
592
+ #### 队列类型选择指南
593
+
594
+ Crawlo支持三种队列类型,可通过`QUEUE_TYPE`配置项设置:
595
+
596
+ - **memory**:使用内存队列,适用于单机模式
597
+ - **redis**:使用Redis队列,适用于分布式模式
598
+ - **auto**:自动检测模式,根据Redis可用性自动选择
599
+
600
+ 推荐使用`auto`模式,让框架根据环境自动选择最适合的队列类型。
601
+
602
+ <!-- 配置系统 section -->
603
+ <h2 align="center">🎛️ 配置系统</h2>
604
+
605
+ ### 传统配置方式
606
+
607
+ ```
608
+ # settings.py
609
+ PROJECT_NAME = 'myproject'
610
+ CONCURRENCY = 16
611
+ DOWNLOAD_DELAY = 1.0
612
+ QUEUE_TYPE = 'memory' # 单机模式
613
+ # QUEUE_TYPE = 'redis' # 分布式模式
614
+
615
+ # Redis 配置 (分布式模式下使用)
616
+ REDIS_HOST = 'localhost'
617
+ REDIS_PORT = 6379
618
+ REDIS_DB = 0
619
+ REDIS_PASSWORD = ''
620
+
621
+ # 数据管道配置
622
+ PIPELINES = [
623
+ 'crawlo.pipelines.console_pipeline.ConsolePipeline',
624
+ 'crawlo.pipelines.json_pipeline.JsonPipeline',
625
+ 'crawlo.pipelines.redis_dedup_pipeline.RedisDedupPipeline', # Redis去重管道
626
+ 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL存储管道
627
+ ]
628
+
629
+ # 高级日志配置
630
+ LOG_FILE = 'logs/spider.log'
631
+ LOG_LEVEL = 'INFO'
632
+ LOG_MAX_BYTES = 10 * 1024 * 1024 # 10MB
633
+ LOG_BACKUP_COUNT = 5
634
+ LOG_JSON_FORMAT = False # 设置为True启用JSON格式
635
+
636
+ # 启用高级日志扩展
637
+ ADVANCED_LOGGING_ENABLED = True
638
+
639
+ # 启用日志监控
640
+ LOG_MONITOR_ENABLED = True
641
+ LOG_MONITOR_INTERVAL = 30
642
+ LOG_MONITOR_DETAILED_STATS = True
643
+
644
+ # 添加扩展
645
+ EXTENSIONS = [
646
+ 'crawlo.extension.log_interval.LogIntervalExtension',
647
+ 'crawlo.extension.log_stats.LogStats',
648
+ 'crawlo.extension.logging_extension.CustomLoggerExtension',
649
+ 'crawlo.extension.memory_monitor.MemoryMonitorExtension',
650
+ ]
651
+ ```
652
+
653
+ ### MySQL 管道配置
654
+
655
+ Crawlo 提供了现成的 MySQL 管道实现,可以轻松将爬取的数据存储到 MySQL 数据库中:
656
+
657
+ ```
658
+ # settings.py 中启用 MySQL 管道
659
+ PIPELINES = [
660
+ 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline',
661
+ ]
662
+
663
+ # MySQL 数据库配置
664
+ MYSQL_HOST = 'localhost'
665
+ MYSQL_PORT = 3306
666
+ MYSQL_USER = 'your_username'
667
+ MYSQL_PASSWORD = 'your_password'
668
+ MYSQL_DB = 'your_database'
669
+ MYSQL_TABLE = 'your_table_name'
670
+
671
+ # 可选的批量插入配置
672
+ MYSQL_BATCH_SIZE = 100
673
+ MYSQL_USE_BATCH = True
674
+ ```
675
+
676
+ MySQL 管道特性:
677
+ - **异步操作**:基于 asyncmy 驱动,提供高性能的异步数据库操作
678
+ - **连接池**:自动管理数据库连接,提高效率
679
+ - **批量插入**:支持批量插入以提高性能
680
+ - **事务支持**:确保数据一致性
681
+ - **灵活配置**:支持自定义表名、批量大小等参数
682
+
683
+ ### 命令行配置
684
+
685
+ ```
686
+ # 运行单个爬虫
687
+ crawlo run myspider
688
+
689
+ # 运行所有爬虫
690
+ crawlo run all
691
+
692
+ # 在项目子目录中也能正确运行
693
+ cd subdirectory
694
+ crawlo run myspider
695
+ ```
696
+
697
+ ---
698
+
699
+ <!-- 核心组件 section -->
700
+ <h2 align="center">🧩 核心组件</h2>
701
+
702
+ ### 中间件系统
703
+ 灵活的中间件系统,支持请求预处理、响应处理和异常处理。
704
+
705
+ ### 管道系统
706
+ 可扩展的数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能:
707
+ - **ConsolePipeline**: 控制台输出管道
708
+ - **JsonPipeline**: JSON文件存储管道
709
+ - **RedisDedupPipeline**: Redis去重管道,基于Redis集合实现分布式去重
710
+ - **AsyncmyMySQLPipeline**: MySQL数据库存储管道,基于asyncmy驱动
711
+
712
+ ### 扩展组件
713
+ 功能增强扩展,包括日志、监控、性能分析等:
714
+ - **LogIntervalExtension**: 定时日志扩展
715
+ - **LogStats**: 统计日志扩展
716
+ - **CustomLoggerExtension**: 自定义日志扩展
717
+ - **MemoryMonitorExtension**: 内存监控扩展
718
+ - **PerformanceProfilerExtension**: 性能分析扩展
719
+ - **HealthCheckExtension**: 健康检查扩展
720
+ - **RequestRecorderExtension**: 请求记录扩展
721
+
722
+ ### 过滤系统
723
+ 智能去重过滤,支持多种去重策略(内存、Redis、Bloom Filter)。
724
+
725
+ ---
726
+
727
+ <!-- 示例项目 section -->
728
+ <h2 align="center">📦 示例项目</h2>
729
+
730
+ - [OFweek分布式爬虫](examples/ofweek_distributed/) - 复杂的分布式爬虫示例,包含Redis去重功能
731
+ - [OFweek独立爬虫](examples/ofweek_standalone/) - 独立运行的爬虫示例
732
+ - [OFweek混合模式爬虫](examples/ofweek_spider/) - 支持单机和分布式模式切换的爬虫示例
733
+
734
+ ---
735
+
736
+ <!-- 文档 section -->
737
+ <h2 align="center">📚 文档</h2>
738
+
739
+ 完整的文档请访问 [Crawlo Documentation](https://crawlo.readthedocs.io/)
740
+
741
+ - [快速开始指南](docs/modules/index.md)
742
+ - [模块化文档](docs/modules/index.md)
743
+ - [核心引擎文档](docs/modules/core/engine.md)
744
+ - [调度器文档](docs/modules/core/scheduler.md)
745
+ - [下载器文档](docs/modules/downloader/index.md)
746
+ - [中间件文档](docs/modules/middleware/index.md)
747
+ - [管道文档](docs/modules/pipeline/index.md)
748
+ - [队列文档](docs/modules/queue/index.md)
749
+ - [过滤器文档](docs/modules/filter/index.md)
750
+ - [扩展组件文档](docs/modules/extension/index.md)
751
+
752
+ ---
753
+
754
+ <!-- 贡献 section -->
755
+ <h2 align="center">🤝 贡献</h2>
756
+
757
+ 欢迎提交 Issue 和 Pull Request 来帮助改进 Crawlo!
758
+
759
+ ---
760
+
761
+ <!-- 许可证 section -->
762
+ <h2 align="center">📄 许可证</h2>
763
+
764
+ 本项目采用 MIT 许可证,详情请见 [LICENSE](LICENSE) 文件。