crawlo 1.2.5__py3-none-any.whl → 1.2.7__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 (209) 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 +75 -88
  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 +138 -144
  12. crawlo/commands/list.py +155 -155
  13. crawlo/commands/run.py +323 -323
  14. crawlo/commands/startproject.py +436 -436
  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 +365 -354
  21. crawlo/core/processor.py +40 -40
  22. crawlo/core/scheduler.py +251 -143
  23. crawlo/crawler.py +1099 -1110
  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 +228 -221
  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 +39 -38
  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 +234 -281
  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 +136 -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 +62 -61
  74. crawlo/pipelines/redis_dedup_pipeline.py +166 -165
  75. crawlo/project.py +314 -279
  76. crawlo/queue/pqueue.py +37 -37
  77. crawlo/queue/queue_manager.py +377 -337
  78. crawlo/queue/redis_priority_queue.py +306 -299
  79. crawlo/settings/__init__.py +7 -7
  80. crawlo/settings/default_settings.py +219 -217
  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/settings.py.tmpl +288 -324
  92. crawlo/templates/project/settings_distributed.py.tmpl +157 -154
  93. crawlo/templates/project/settings_gentle.py.tmpl +101 -128
  94. crawlo/templates/project/settings_high_performance.py.tmpl +135 -150
  95. crawlo/templates/project/settings_simple.py.tmpl +99 -103
  96. crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
  97. crawlo/templates/run.py.tmpl +45 -47
  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 +143 -106
  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 +351 -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.5.dist-info → crawlo-1.2.7.dist-info}/METADATA +764 -764
  130. crawlo-1.2.7.dist-info/RECORD +209 -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_consistency.py +81 -0
  152. tests/test_config_validator.py +193 -193
  153. tests/test_crawlo_proxy_integration.py +172 -172
  154. tests/test_date_tools.py +123 -123
  155. tests/test_default_header_middleware.py +158 -158
  156. tests/test_double_crawlo_fix.py +207 -207
  157. tests/test_double_crawlo_fix_simple.py +124 -124
  158. tests/test_download_delay_middleware.py +221 -221
  159. tests/test_downloader_proxy_compatibility.py +268 -268
  160. tests/test_dynamic_downloaders_proxy.py +124 -124
  161. tests/test_dynamic_proxy.py +92 -92
  162. tests/test_dynamic_proxy_config.py +146 -146
  163. tests/test_dynamic_proxy_real.py +109 -109
  164. tests/test_edge_cases.py +303 -303
  165. tests/test_enhanced_error_handler.py +270 -270
  166. tests/test_env_config.py +121 -121
  167. tests/test_error_handler_compatibility.py +112 -112
  168. tests/test_final_validation.py +153 -153
  169. tests/test_framework_env_usage.py +103 -103
  170. tests/test_integration.py +356 -356
  171. tests/test_item_dedup_redis_key.py +122 -122
  172. tests/test_mode_consistency.py +52 -0
  173. tests/test_offsite_middleware.py +221 -221
  174. tests/test_parsel.py +29 -29
  175. tests/test_performance.py +327 -327
  176. tests/test_proxy_api.py +264 -264
  177. tests/test_proxy_health_check.py +32 -32
  178. tests/test_proxy_middleware.py +121 -121
  179. tests/test_proxy_middleware_enhanced.py +216 -216
  180. tests/test_proxy_middleware_integration.py +136 -136
  181. tests/test_proxy_providers.py +56 -56
  182. tests/test_proxy_stats.py +19 -19
  183. tests/test_proxy_strategies.py +59 -59
  184. tests/test_queue_manager_double_crawlo.py +173 -173
  185. tests/test_queue_manager_redis_key.py +176 -176
  186. tests/test_real_scenario_proxy.py +195 -195
  187. tests/test_redis_config.py +28 -28
  188. tests/test_redis_connection_pool.py +294 -294
  189. tests/test_redis_key_naming.py +181 -181
  190. tests/test_redis_key_validator.py +123 -123
  191. tests/test_redis_queue.py +224 -224
  192. tests/test_request_ignore_middleware.py +182 -182
  193. tests/test_request_serialization.py +70 -70
  194. tests/test_response_code_middleware.py +349 -349
  195. tests/test_response_filter_middleware.py +427 -427
  196. tests/test_response_improvements.py +152 -152
  197. tests/test_retry_middleware.py +241 -241
  198. tests/test_scheduler.py +252 -241
  199. tests/test_scheduler_config_update.py +134 -0
  200. tests/test_simple_response.py +61 -61
  201. tests/test_telecom_spider_redis_key.py +205 -205
  202. tests/test_template_content.py +87 -87
  203. tests/test_template_redis_key.py +134 -134
  204. tests/test_tools.py +153 -153
  205. tests/tools_example.py +257 -257
  206. crawlo-1.2.5.dist-info/RECORD +0 -206
  207. {crawlo-1.2.5.dist-info → crawlo-1.2.7.dist-info}/WHEEL +0 -0
  208. {crawlo-1.2.5.dist-info → crawlo-1.2.7.dist-info}/entry_points.txt +0 -0
  209. {crawlo-1.2.5.dist-info → crawlo-1.2.7.dist-info}/top_level.txt +0 -0
@@ -1,764 +1,764 @@
1
- Metadata-Version: 2.4
2
- Name: crawlo
3
- Version: 1.2.5
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) 文件。
1
+ Metadata-Version: 2.4
2
+ Name: crawlo
3
+ Version: 1.2.7
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) 文件。