crawlo 1.1.9__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 (221) 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 -345
  21. crawlo/core/processor.py +40 -40
  22. crawlo/core/scheduler.py +143 -136
  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 -334
  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.1.dist-info/METADATA +692 -0
  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 +174 -231
  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.1.9.dist-info/METADATA +0 -626
  218. crawlo-1.1.9.dist-info/RECORD +0 -190
  219. {crawlo-1.1.9.dist-info → crawlo-1.2.1.dist-info}/WHEEL +0 -0
  220. {crawlo-1.1.9.dist-info → crawlo-1.2.1.dist-info}/entry_points.txt +0 -0
  221. {crawlo-1.1.9.dist-info → crawlo-1.2.1.dist-info}/top_level.txt +0 -0
@@ -1,626 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: crawlo
3
- Version: 1.1.9
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
- # Crawlo - 异步分布式爬虫框架
52
-
53
- <div align="center">
54
-
55
- [![Python Version](https://img.shields.io/badge/python-3.8%2B-blue)](https://www.python.org/downloads/)
56
- [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
57
- [![Documentation](https://img.shields.io/badge/docs-latest-brightgreen)](https://crawlo.readthedocs.io/)
58
-
59
- 一个基于 asyncio 的高性能异步分布式爬虫框架,支持单机和分布式部署。
60
-
61
- </div>
62
-
63
- ## 🌟 特性
64
-
65
- - **异步高性能**: 基于 asyncio 实现,充分利用现代 CPU 多核性能
66
- - **分布式支持**: 内置 Redis 队列,轻松实现分布式部署
67
- - **模块化设计**: 中间件、管道、扩展组件系统,易于定制和扩展
68
- - **智能去重**: 多种去重策略(内存、Redis、Bloom Filter)
69
- - **灵活配置**: 支持多种配置方式,适应不同场景需求
70
- - **丰富文档**: 完整的中英文双语文档和示例项目
71
-
72
- ## 🚀 快速开始
73
-
74
- ### 安装
75
-
76
- ```bash
77
- pip install crawlo
78
- ```
79
-
80
- ### 创建项目
81
-
82
- ```bash
83
- # 创建默认项目
84
- crawlo startproject myproject
85
-
86
- # 创建分布式模板项目
87
- crawlo startproject myproject distributed
88
-
89
- # 创建项目并选择特定模块
90
- crawlo startproject myproject --modules mysql,redis,proxy
91
-
92
- cd myproject
93
- ```
94
-
95
- ### 生成爬虫
96
-
97
- ```bash
98
- # 在项目目录中生成爬虫
99
- crawlo genspider news_spider news.example.com
100
- ```
101
-
102
- ### 编写爬虫
103
-
104
- ```python
105
- from crawlo import Spider, Request, Item
106
-
107
- class MyItem(Item):
108
- title = ''
109
- url = ''
110
-
111
- class MySpider(Spider):
112
- name = 'myspider'
113
-
114
- async def start_requests(self):
115
- yield Request('https://httpbin.org/get', callback=self.parse)
116
-
117
- async def parse(self, response):
118
- yield MyItem(
119
- title='Example Title',
120
- url=response.url
121
- )
122
- ```
123
-
124
- ### 运行爬虫
125
-
126
- ```bash
127
- # 使用命令行工具运行爬虫(推荐)
128
- crawlo run myspider
129
-
130
- # 使用项目自带的 run.py 脚本运行
131
- python run.py
132
-
133
- # 运行所有爬虫
134
- crawlo run all
135
-
136
- # 在项目子目录中也能正确运行
137
- cd subdirectory
138
- crawlo run myspider
139
- ```
140
-
141
- ## 📜 命令行工具
142
-
143
- Crawlo 提供了丰富的命令行工具来帮助开发和管理爬虫项目:
144
-
145
- ### 获取帮助
146
-
147
- ```bash
148
- # 显示帮助信息
149
- crawlo -h
150
- crawlo --help
151
- crawlo help
152
- ```
153
-
154
- ### crawlo startproject
155
-
156
- 创建新的爬虫项目。
157
-
158
- ```bash
159
- # 基本用法
160
- crawlo startproject <project_name> [template_type] [--modules module1,module2]
161
-
162
- # 示例
163
- crawlo startproject my_spider_project
164
- crawlo startproject news_crawler simple
165
- crawlo startproject ecommerce_spider distributed --modules mysql,proxy
166
- ```
167
-
168
- **参数说明:**
169
- - `project_name`: 项目名称(必须是有效的Python标识符)
170
- - `template_type`: 模板类型(可选)
171
- - `default`: 默认模板 - 通用配置,适合大多数项目
172
- - `simple`: 简化模板 - 最小配置,适合快速开始
173
- - `distributed`: 分布式模板 - 针对分布式爬取优化
174
- - `high-performance`: 高性能模板 - 针对大规模高并发优化
175
- - `gentle`: 温和模板 - 低负载配置,对目标网站友好
176
- - `--modules`: 选择要包含的模块组件(可选)
177
- - `mysql`: MySQL数据库支持
178
- - `mongodb`: MongoDB数据库支持
179
- - `redis`: Redis支持(分布式队列和去重)
180
- - `proxy`: 代理支持
181
- - `monitoring`: 监控和性能分析
182
- - `dedup`: 去重功能
183
- - `httpx`: HttpX下载器
184
- - `aiohttp`: AioHttp下载器
185
- - `curl`: CurlCffi下载器
186
-
187
- ### crawlo genspider
188
-
189
- 在现有项目中生成新的爬虫。
190
-
191
- ```bash
192
- # 基本用法
193
- crawlo genspider <spider_name> <domain>
194
-
195
- # 示例
196
- crawlo genspider news_spider news.example.com
197
- crawlo genspider product_spider shop.example.com
198
- ```
199
-
200
- **参数说明:**
201
- - `spider_name`: 爬虫名称(必须是有效的Python标识符)
202
- - `domain`: 目标域名
203
-
204
- ### crawlo run
205
-
206
- 运行爬虫。
207
-
208
- ```bash
209
- # 基本用法
210
- crawlo run <spider_name>|all [--json] [--no-stats]
211
-
212
- # 示例
213
- crawlo run myspider
214
- crawlo run all
215
- crawlo run all --json --no-stats
216
- ```
217
-
218
- **参数说明:**
219
- - `spider_name`: 要运行的爬虫名称
220
- - `all`: 运行所有爬虫
221
- - `--json`: 以JSON格式输出结果
222
- - `--no-stats`: 不记录统计信息
223
-
224
- ### crawlo list
225
-
226
- 列出项目中所有可用的爬虫。
227
-
228
- ```bash
229
- # 基本用法
230
- crawlo list [--json]
231
-
232
- # 示例
233
- crawlo list
234
- crawlo list --json
235
- ```
236
-
237
- **参数说明:**
238
- - `--json`: 以JSON格式输出结果
239
-
240
- ### crawlo check
241
-
242
- 检查爬虫定义的合规性。
243
-
244
- ```bash
245
- # 基本用法
246
- crawlo check [--fix] [--ci] [--json] [--watch]
247
-
248
- # 示例
249
- crawlo check
250
- crawlo check --fix
251
- crawlo check --ci
252
- crawlo check --watch
253
- ```
254
-
255
- **参数说明:**
256
- - `--fix`: 自动修复常见问题
257
- - `--ci`: CI模式输出(简洁格式)
258
- - `--json`: 以JSON格式输出结果
259
- - `--watch`: 监听模式,文件更改时自动检查
260
-
261
- ### crawlo stats
262
-
263
- 查看爬虫运行统计信息。
264
-
265
- ```bash
266
- # 基本用法
267
- crawlo stats [spider_name] [--all]
268
-
269
- # 示例
270
- crawlo stats
271
- crawlo stats myspider
272
- crawlo stats myspider --all
273
- ```
274
-
275
- **参数说明:**
276
- - `spider_name`: 指定要查看统计信息的爬虫名称
277
- - `--all`: 显示指定爬虫的所有历史运行记录
278
-
279
- ## 🏗️ 架构设计
280
-
281
- ### 组件交互图
282
-
283
- ```
284
- ┌─────────────────────────────────────────────────────────────────────┐
285
- │ Crawler │
286
- │ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────────┐ │
287
- │ │ Spider │ │ Engine │ │ ExtensionManager │ │
288
- │ │ │ │ │ │ │ │
289
- │ │ start_urls │ │ Scheduler ◄─┼──┼──► StatsCollector │ │
290
- │ │ parse() │ │ │ │ │ │
291
- │ │ │ │ Downloader ◄─┼──┼──► MiddlewareManager │ │
292
- │ │ │ │ │ │ │ │
293
- │ │ │ │ Processor ◄─┼──┼──► PipelineManager │ │
294
- │ └──────────────┘ └──────┬───────┘ └──────────────────────────┘ │
295
- └──────────────────────────┼─────────────────────────────────────────┘
296
-
297
- ┌──────────────────▼──────────────────┐
298
- │ Scheduler │
299
- │ ┌──────────────────────────────┐ │
300
- │ │ QueueManager │ │
301
- │ │ ┌─────────┐ ┌────────────┐ │ │
302
- │ │ │ Memory │ │ Redis │ │ │
303
- │ │ │ Queue │ │ Queue │ │ │
304
- │ │ └─────────┘ └────────────┘ │ │
305
- │ └──────────────────────────────┘ │
306
- │ ┌──────────────────────────────┐ │
307
- │ │ Filter │ │
308
- │ │ ┌─────────┐ ┌────────────┐ │ │
309
- │ │ │ Memory │ │ Redis │ │ │
310
- │ │ │ Filter │ │ Filter │ │ │
311
- │ │ └─────────┘ └────────────┘ │ │
312
- │ └──────────────────────────────┘ │
313
- └─────────────────────────────────────┘
314
-
315
- ┌──────────────────▼──────────────────┐
316
- │ Downloader │
317
- │ ┌──────────────────────────────┐ │
318
- │ │ MiddlewareManager │ │
319
- │ │ │ │
320
- │ │ RequestMiddleware ◄────────┐ │ │
321
- │ │ ResponseMiddleware │ │ │
322
- │ │ ExceptionMiddleware │ │ │
323
- │ │ ╱ │ │
324
- │ └─────────────────────────╱───┘ │
325
- │ ╱ │
326
- │ ┌───────────────────────▼──┐ │
327
- │ │ Download Implementations │ │
328
- │ │ - AioHttpDownloader │ │
329
- │ │ - HttpXDownloader │ │
330
- │ │ - CurlCffiDownloader │ │
331
- │ └──────────────────────────┘ │
332
- └─────────────────────────────────────┘
333
-
334
- ┌──────────────────▼──────────────────┐
335
- │ Processor │
336
- │ ┌──────────────────────────────┐ │
337
- │ │ PipelineManager │ │
338
- │ │ ┌─────────────────────────┐ │ │
339
- │ │ │ Pipeline Stages │ │ │
340
- │ │ │ - ValidationPipeline │ │ │
341
- │ │ │ - ProcessingPipeline │ │ │
342
- │ │ │ - StoragePipeline │ │ │
343
- │ │ │ - DeduplicationPipeline │ │ │
344
- │ │ └─────────────────────────┘ │ │
345
- │ └──────────────────────────────┘ │
346
- └─────────────────────────────────────┘
347
- ```
348
-
349
- ### 运行模式切换图
350
-
351
- ```
352
- ┌─────────────────────┐
353
- │ ModeManager │
354
- │ (运行模式管理器) │
355
- └─────────┬───────────┘
356
-
357
- ┌─────────────────────┼─────────────────────┐
358
- │ │ │
359
- ▼ ▼ ▼
360
- ┌───────────────┐ ┌─────────────────┐ ┌─────────────────┐
361
- │ Standalone │ │ Distributed │ │ Auto │
362
- │ (单机模式) │ │ (分布式模式) │ │ (自动检测模式) │
363
- └───────┬───────┘ └─────────┬───────┘ └─────────┬───────┘
364
- │ │ │
365
- ▼ ▼ ▼
366
- ┌───────────────┐ ┌─────────────────┐ ┌─────────────────┐
367
- │ Memory Queue │ │ Redis Queue │ │ Auto Select │
368
- │ Memory Filter │ │ Redis Filter │ │ Memory/Redis │
369
- └───────────────┘ └─────────────────┘ └─────────────────┘
370
- ```
371
-
372
- ### 数据流向图
373
-
374
- ```
375
- ┌─────────────┐ 1.生成初始请求 ┌──────────────┐
376
- │ Spider ├─────────────────────►│ Scheduler │
377
- └─────────────┘ └──────┬───────┘
378
- │ 2.去重检查
379
-
380
- ┌─────────────────┐
381
- │ Filter │
382
- └─────────┬───────┘
383
- │ 3.入队
384
-
385
- ┌─────────────────┐
386
- │ Queue │
387
- └─────────┬───────┘
388
- │ 4.获取请求
389
-
390
- ┌─────────────────┐ 5.下载请求
391
- │ Downloader ├──────────────────┐
392
- └─────────────────┘ │
393
- │ 6.解析响应 │
394
- ▼ ▼
395
- ┌─────────────────┐ 7.生成数据 ┌─────────────┐
396
- │ Processor ├────────────────►│ Pipeline │
397
- └─────────────────┘ └──────┬──────┘
398
- │ 8.存储数据 │ 9.去重处理
399
- ▼ ▼
400
- ┌─────────────────┐ ┌─────────────────┐
401
- │ Items │◄─────────────┤ Deduplication │
402
- └─────────────────┘ │ Pipeline │
403
- └─────────────────┘
404
- ```
405
-
406
- ### 模块层次结构图
407
-
408
- ```
409
- crawlo/
410
- ├── cli.py # 命令行接口
411
- ├── crawler.py # 爬虫运行实例
412
- ├── project.py # 项目管理
413
- ├── config.py # 配置管理
414
- ├── mode_manager.py # 运行模式管理器
415
- ├── stats_collector.py # 统计收集器
416
- ├── subscriber.py # 事件订阅器
417
- ├── task_manager.py # 任务管理器
418
- ├── event.py # 事件定义
419
- ├── exceptions.py # 异常定义
420
- ├──
421
- ├── core/ # 核心组件
422
- │ ├── engine.py # 引擎
423
- │ ├── scheduler.py # 调度器
424
- │ ├── processor.py # 处理器
425
-
426
- ├── spider/ # 爬虫基类
427
- │ └── __init__.py # 爬虫元类和基类
428
-
429
- ├── network/ # 网络相关
430
- │ ├── request.py # 请求对象
431
- │ └── response.py # 响应对象
432
-
433
- ├── downloader/ # 下载器
434
- │ ├── __init__.py # 下载器基类
435
- │ ├── aiohttp_downloader.py # AioHttp实现
436
- │ ├── httpx_downloader.py # HttpX实现
437
- │ └── cffi_downloader.py # CurlCffi实现
438
-
439
- ├── queue/ # 队列管理
440
- │ ├── __init__.py
441
- │ ├── queue_manager.py # 队列管理器
442
- │ ├── pqueue.py # 内存优先队列
443
- │ └── redis_priority_queue.py # Redis优先队列
444
-
445
- ├── filters/ # 过滤器
446
- │ ├── __init__.py
447
- │ ├── base_filter.py # 过滤器基类
448
- │ ├── memory_filter.py # 内存过滤器
449
- │ └── aioredis_filter.py # Redis过滤器
450
-
451
- ├── middleware/ # 中间件
452
- │ ├── __init__.py
453
- │ ├── middleware_manager.py # 中间件管理器
454
- │ ├── default_header.py # 默认请求头
455
- │ ├── download_delay.py # 下载延迟
456
- │ ├── proxy.py # 代理支持
457
- │ ├── request_ignore.py # 请求忽略
458
- │ ├── response_code.py # 响应码处理
459
- │ ├── response_filter.py # 响应过滤
460
- │ └── retry.py # 重试机制
461
-
462
- ├── pipelines/ # 数据管道
463
- │ ├── __init__.py
464
- │ ├── pipeline_manager.py # 管道管理器
465
- │ ├── base_pipeline.py # 管道基类
466
- │ ├── console_pipeline.py # 控制台输出管道
467
- │ ├── json_pipeline.py # JSON存储管道
468
- │ ├── redis_dedup_pipeline.py # Redis去重管道
469
- │ └── mysql_pipeline.py # MySQL存储管道
470
-
471
- ├── extension/ # 扩展组件
472
- │ ├── __init__.py
473
- │ ├── log_interval.py # 定时日志
474
- │ ├── log_stats.py # 统计日志
475
- │ ├── logging_extension.py # 日志扩展
476
- │ ├── memory_monitor.py # 内存监控
477
- │ └── performance_profiler.py # 性能分析
478
-
479
- ├── settings/ # 配置系统
480
- │ ├── __init__.py
481
- │ ├── default_settings.py # 默认配置
482
- │ └── setting_manager.py # 配置管理器
483
-
484
- ├── utils/ # 工具库
485
- │ ├── __init__.py
486
- │ ├── log.py # 日志工具
487
- │ ├── request.py # 请求工具
488
- │ ├── request_serializer.py # 请求序列化
489
- │ └── func_tools.py # 函数工具
490
-
491
- └── templates/ # 模板文件
492
- ├── project/
493
- └── spider/
494
- ```
495
-
496
- ### 组件说明
497
-
498
- - **Crawler**: 爬虫运行实例,管理Spider与引擎的生命周期
499
- - **Engine**: 引擎组件,协调Scheduler、Downloader、Processor
500
- - **Scheduler**: 调度器,管理请求队列和去重过滤
501
- - **Downloader**: 下载器,负责网络请求,支持多种实现(aiohttp, httpx, curl-cffi)
502
- - **Processor**: 处理器,处理响应数据和管道
503
- - **QueueManager**: 统一的队列管理器,支持内存队列和Redis队列的自动切换
504
- - **Filter**: 请求去重过滤器,支持内存和Redis两种实现
505
- - **Middleware**: 中间件系统,处理请求/响应的预处理和后处理
506
- - **Pipeline**: 数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能
507
- - **Spider**: 爬虫基类,定义爬取逻辑
508
-
509
- ### 运行模式
510
-
511
- Crawlo支持三种运行模式:
512
- - **standalone**: 单机模式,使用内存队列和内存过滤器
513
- - **distributed**: 分布式模式,使用Redis队列和Redis过滤器
514
- - **auto**: 自动检测模式,根据环境自动选择最佳运行方式
515
-
516
- ## 🎛️ 配置系统
517
-
518
- ### 传统配置方式
519
-
520
- ```
521
- # settings.py
522
- PROJECT_NAME = 'myproject'
523
- CONCURRENCY = 16
524
- DOWNLOAD_DELAY = 1.0
525
- QUEUE_TYPE = 'memory' # 单机模式
526
- # QUEUE_TYPE = 'redis' # 分布式模式
527
-
528
- # Redis 配置 (分布式模式下使用)
529
- REDIS_HOST = 'localhost'
530
- REDIS_PORT = 6379
531
- REDIS_DB = 0
532
- REDIS_PASSWORD = ''
533
-
534
- # 数据管道配置
535
- PIPELINES = [
536
- 'crawlo.pipelines.console_pipeline.ConsolePipeline',
537
- 'crawlo.pipelines.json_pipeline.JsonPipeline',
538
- 'crawlo.pipelines.redis_dedup_pipeline.RedisDedupPipeline', # Redis去重管道
539
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL存储管道
540
- ]
541
- ```
542
-
543
- ### MySQL 管道配置
544
-
545
- Crawlo 提供了现成的 MySQL 管道实现,可以轻松将爬取的数据存储到 MySQL 数据库中:
546
-
547
- ```python
548
- # 在 settings.py 中启用 MySQL 管道
549
- PIPELINES = [
550
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline',
551
- ]
552
-
553
- # MySQL 数据库配置
554
- MYSQL_HOST = 'localhost'
555
- MYSQL_PORT = 3306
556
- MYSQL_USER = 'your_username'
557
- MYSQL_PASSWORD = 'your_password'
558
- MYSQL_DB = 'your_database'
559
- MYSQL_TABLE = 'your_table_name'
560
-
561
- # 可选的批量插入配置
562
- MYSQL_BATCH_SIZE = 100
563
- MYSQL_USE_BATCH = True
564
- ```
565
-
566
- MySQL 管道特性:
567
- - **异步操作**:基于 asyncmy 驱动,提供高性能的异步数据库操作
568
- - **连接池**:自动管理数据库连接,提高效率
569
- - **批量插入**:支持批量插入以提高性能
570
- - **事务支持**:确保数据一致性
571
- - **灵活配置**:支持自定义表名、批量大小等参数
572
-
573
- ### 命令行配置
574
-
575
- ``bash
576
- # 运行单个爬虫
577
- crawlo run myspider
578
-
579
- # 运行所有爬虫
580
- crawlo run all
581
-
582
- # 在项目子目录中也能正确运行
583
- cd subdirectory
584
- crawlo run myspider
585
- ```
586
-
587
- ## 🧩 核心组件
588
-
589
- ### 中间件系统
590
- 灵活的中间件系统,支持请求预处理、响应处理和异常处理。
591
-
592
- ### 管道系统
593
- 可扩展的数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能:
594
- - **ConsolePipeline**: 控制台输出管道
595
- - **JsonPipeline**: JSON文件存储管道
596
- - **RedisDedupPipeline**: Redis去重管道,基于Redis集合实现分布式去重
597
- - **AsyncmyMySQLPipeline**: MySQL数据库存储管道,基于asyncmy驱动
598
-
599
- ### 扩展组件
600
- 功能增强扩展,包括日志、监控、性能分析等。
601
-
602
- ### 过滤系统
603
- 智能去重过滤,支持多种去重策略(内存、Redis、Bloom Filter)。
604
-
605
- ## 📦 示例项目
606
-
607
- - [API数据采集](examples/api_data_collection/) - 简单的API数据采集示例
608
- - [电信设备许可证](examples/telecom_licenses_distributed/) - 分布式爬取示例
609
- - [OFweek分布式爬虫](examples/ofweek_distributed/) - 复杂的分布式爬虫示例,包含Redis去重功能
610
-
611
- ## 📚 文档
612
-
613
- 完整的文档请访问 [Crawlo Documentation](https://crawlo.readthedocs.io/)
614
-
615
- - [快速开始指南](docs/modules/index.md)
616
- - [模块化文档](docs/modules/index.md)
617
- - [API参考](docs/api_reference.md)
618
- - [配置最佳实践](docs/configuration_best_practices.md)
619
-
620
- ## 🤝 贡献
621
-
622
- 欢迎提交 Issue 和 Pull Request 来帮助改进 Crawlo!
623
-
624
- ## 📄 许可证
625
-
626
- 本项目采用 MIT 许可证,详情请见 [LICENSE](LICENSE) 文件。