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