crawlo 1.4.1__py3-none-any.whl → 1.4.3__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 (326) hide show
  1. crawlo/__init__.py +93 -93
  2. crawlo/__version__.py +1 -1
  3. crawlo/cli.py +75 -75
  4. crawlo/commands/__init__.py +14 -14
  5. crawlo/commands/check.py +594 -594
  6. crawlo/commands/genspider.py +151 -151
  7. crawlo/commands/help.py +138 -138
  8. crawlo/commands/list.py +155 -155
  9. crawlo/commands/run.py +341 -341
  10. crawlo/commands/startproject.py +436 -436
  11. crawlo/commands/stats.py +187 -187
  12. crawlo/commands/utils.py +196 -196
  13. crawlo/config.py +312 -312
  14. crawlo/config_validator.py +277 -277
  15. crawlo/core/__init__.py +52 -52
  16. crawlo/core/engine.py +438 -439
  17. crawlo/core/processor.py +47 -47
  18. crawlo/core/scheduler.py +291 -257
  19. crawlo/crawler.py +650 -650
  20. crawlo/data/__init__.py +5 -5
  21. crawlo/data/user_agents.py +194 -194
  22. crawlo/downloader/__init__.py +273 -273
  23. crawlo/downloader/aiohttp_downloader.py +233 -228
  24. crawlo/downloader/cffi_downloader.py +245 -245
  25. crawlo/downloader/httpx_downloader.py +259 -259
  26. crawlo/downloader/hybrid_downloader.py +212 -212
  27. crawlo/downloader/playwright_downloader.py +402 -402
  28. crawlo/downloader/selenium_downloader.py +472 -472
  29. crawlo/event.py +11 -11
  30. crawlo/exceptions.py +81 -81
  31. crawlo/extension/__init__.py +63 -63
  32. crawlo/extension/health_check.py +141 -141
  33. crawlo/extension/log_interval.py +94 -94
  34. crawlo/extension/log_stats.py +70 -70
  35. crawlo/extension/logging_extension.py +61 -61
  36. crawlo/extension/memory_monitor.py +104 -104
  37. crawlo/extension/performance_profiler.py +133 -133
  38. crawlo/extension/request_recorder.py +107 -107
  39. crawlo/factories/__init__.py +27 -27
  40. crawlo/factories/base.py +68 -68
  41. crawlo/factories/crawler.py +103 -103
  42. crawlo/factories/registry.py +84 -84
  43. crawlo/filters/__init__.py +154 -154
  44. crawlo/filters/aioredis_filter.py +257 -257
  45. crawlo/filters/memory_filter.py +269 -269
  46. crawlo/framework.py +292 -292
  47. crawlo/initialization/__init__.py +44 -44
  48. crawlo/initialization/built_in.py +425 -425
  49. crawlo/initialization/context.py +141 -141
  50. crawlo/initialization/core.py +193 -193
  51. crawlo/initialization/phases.py +148 -148
  52. crawlo/initialization/registry.py +145 -145
  53. crawlo/items/__init__.py +23 -23
  54. crawlo/items/base.py +23 -23
  55. crawlo/items/fields.py +52 -52
  56. crawlo/items/items.py +104 -104
  57. crawlo/logging/__init__.py +45 -37
  58. crawlo/logging/async_handler.py +181 -0
  59. crawlo/logging/config.py +196 -96
  60. crawlo/logging/factory.py +171 -128
  61. crawlo/logging/manager.py +111 -111
  62. crawlo/logging/monitor.py +153 -0
  63. crawlo/logging/sampler.py +167 -0
  64. crawlo/middleware/__init__.py +21 -21
  65. crawlo/middleware/default_header.py +132 -132
  66. crawlo/middleware/download_delay.py +104 -104
  67. crawlo/middleware/middleware_manager.py +135 -135
  68. crawlo/middleware/offsite.py +123 -123
  69. crawlo/middleware/proxy.py +386 -386
  70. crawlo/middleware/request_ignore.py +86 -86
  71. crawlo/middleware/response_code.py +150 -150
  72. crawlo/middleware/response_filter.py +136 -136
  73. crawlo/middleware/retry.py +124 -124
  74. crawlo/middleware/simple_proxy.py +65 -65
  75. crawlo/mode_manager.py +219 -219
  76. crawlo/network/__init__.py +21 -21
  77. crawlo/network/request.py +379 -379
  78. crawlo/network/response.py +359 -359
  79. crawlo/pipelines/__init__.py +21 -21
  80. crawlo/pipelines/bloom_dedup_pipeline.py +146 -146
  81. crawlo/pipelines/console_pipeline.py +39 -39
  82. crawlo/pipelines/csv_pipeline.py +316 -316
  83. crawlo/pipelines/database_dedup_pipeline.py +197 -197
  84. crawlo/pipelines/json_pipeline.py +218 -218
  85. crawlo/pipelines/memory_dedup_pipeline.py +105 -105
  86. crawlo/pipelines/mongo_pipeline.py +131 -131
  87. crawlo/pipelines/mysql_pipeline.py +325 -325
  88. crawlo/pipelines/pipeline_manager.py +100 -84
  89. crawlo/pipelines/redis_dedup_pipeline.py +156 -156
  90. crawlo/project.py +349 -338
  91. crawlo/queue/pqueue.py +42 -42
  92. crawlo/queue/queue_manager.py +526 -522
  93. crawlo/queue/redis_priority_queue.py +370 -367
  94. crawlo/settings/__init__.py +7 -7
  95. crawlo/settings/default_settings.py +284 -284
  96. crawlo/settings/setting_manager.py +219 -219
  97. crawlo/spider/__init__.py +657 -657
  98. crawlo/stats_collector.py +73 -73
  99. crawlo/subscriber.py +129 -129
  100. crawlo/task_manager.py +138 -138
  101. crawlo/templates/crawlo.cfg.tmpl +10 -10
  102. crawlo/templates/project/__init__.py.tmpl +3 -3
  103. crawlo/templates/project/items.py.tmpl +17 -17
  104. crawlo/templates/project/middlewares.py.tmpl +118 -118
  105. crawlo/templates/project/pipelines.py.tmpl +96 -96
  106. crawlo/templates/project/settings.py.tmpl +170 -170
  107. crawlo/templates/project/settings_distributed.py.tmpl +169 -169
  108. crawlo/templates/project/settings_gentle.py.tmpl +166 -166
  109. crawlo/templates/project/settings_high_performance.py.tmpl +167 -167
  110. crawlo/templates/project/settings_minimal.py.tmpl +65 -65
  111. crawlo/templates/project/settings_simple.py.tmpl +164 -164
  112. crawlo/templates/project/spiders/__init__.py.tmpl +9 -9
  113. crawlo/templates/run.py.tmpl +34 -34
  114. crawlo/templates/spider/spider.py.tmpl +143 -143
  115. crawlo/templates/spiders_init.py.tmpl +9 -9
  116. crawlo/tools/__init__.py +200 -200
  117. crawlo/tools/anti_crawler.py +268 -268
  118. crawlo/tools/authenticated_proxy.py +240 -240
  119. crawlo/tools/data_formatter.py +225 -225
  120. crawlo/tools/data_validator.py +180 -180
  121. crawlo/tools/date_tools.py +289 -289
  122. crawlo/tools/distributed_coordinator.py +384 -384
  123. crawlo/tools/encoding_converter.py +127 -127
  124. crawlo/tools/network_diagnostic.py +364 -364
  125. crawlo/tools/request_tools.py +82 -82
  126. crawlo/tools/retry_mechanism.py +224 -224
  127. crawlo/tools/scenario_adapter.py +262 -262
  128. crawlo/tools/text_cleaner.py +232 -232
  129. crawlo/utils/__init__.py +34 -34
  130. crawlo/utils/batch_processor.py +259 -259
  131. crawlo/utils/class_loader.py +25 -25
  132. crawlo/utils/controlled_spider_mixin.py +439 -439
  133. crawlo/utils/db_helper.py +343 -343
  134. crawlo/utils/enhanced_error_handler.py +356 -356
  135. crawlo/utils/env_config.py +142 -142
  136. crawlo/utils/error_handler.py +165 -165
  137. crawlo/utils/fingerprint.py +122 -122
  138. crawlo/utils/func_tools.py +82 -82
  139. crawlo/utils/large_scale_config.py +286 -286
  140. crawlo/utils/large_scale_helper.py +344 -344
  141. crawlo/utils/log.py +79 -79
  142. crawlo/utils/performance_monitor.py +285 -285
  143. crawlo/utils/queue_helper.py +175 -175
  144. crawlo/utils/redis_connection_pool.py +388 -388
  145. crawlo/utils/redis_key_validator.py +198 -198
  146. crawlo/utils/request.py +267 -267
  147. crawlo/utils/request_serializer.py +225 -225
  148. crawlo/utils/spider_loader.py +61 -61
  149. crawlo/utils/system.py +11 -11
  150. crawlo/utils/tools.py +4 -4
  151. crawlo/utils/url.py +39 -39
  152. crawlo-1.4.3.dist-info/METADATA +190 -0
  153. crawlo-1.4.3.dist-info/RECORD +326 -0
  154. examples/__init__.py +7 -7
  155. examples/test_project/__init__.py +7 -7
  156. examples/test_project/run.py +34 -34
  157. examples/test_project/test_project/__init__.py +3 -3
  158. examples/test_project/test_project/items.py +17 -17
  159. examples/test_project/test_project/middlewares.py +118 -118
  160. examples/test_project/test_project/pipelines.py +96 -96
  161. examples/test_project/test_project/settings.py +169 -169
  162. examples/test_project/test_project/spiders/__init__.py +9 -9
  163. examples/test_project/test_project/spiders/of_week_dis.py +143 -143
  164. tests/__init__.py +7 -7
  165. tests/advanced_tools_example.py +275 -275
  166. tests/authenticated_proxy_example.py +106 -106
  167. tests/baidu_performance_test.py +108 -108
  168. tests/baidu_test.py +59 -59
  169. tests/cleaners_example.py +160 -160
  170. tests/comprehensive_framework_test.py +212 -212
  171. tests/comprehensive_test.py +81 -81
  172. tests/comprehensive_testing_summary.md +186 -186
  173. tests/config_validation_demo.py +142 -142
  174. tests/controlled_spider_example.py +205 -205
  175. tests/date_tools_example.py +180 -180
  176. tests/debug_configure.py +69 -69
  177. tests/debug_framework_logger.py +84 -84
  178. tests/debug_log_config.py +126 -126
  179. tests/debug_log_levels.py +63 -63
  180. tests/debug_pipelines.py +66 -66
  181. tests/detailed_log_test.py +233 -233
  182. tests/distributed_test.py +66 -66
  183. tests/distributed_test_debug.py +76 -76
  184. tests/dynamic_loading_example.py +523 -523
  185. tests/dynamic_loading_test.py +104 -104
  186. tests/env_config_example.py +133 -133
  187. tests/error_handling_example.py +171 -171
  188. tests/final_comprehensive_test.py +151 -151
  189. tests/final_log_test.py +260 -260
  190. tests/final_validation_test.py +182 -182
  191. tests/fix_log_test.py +142 -142
  192. tests/framework_performance_test.py +202 -202
  193. tests/log_buffering_test.py +111 -111
  194. tests/log_generation_timing_test.py +153 -153
  195. tests/optimized_performance_test.py +211 -211
  196. tests/performance_comparison.py +245 -245
  197. tests/queue_blocking_test.py +113 -113
  198. tests/queue_test.py +89 -89
  199. tests/redis_key_validation_demo.py +130 -130
  200. tests/request_params_example.py +150 -150
  201. tests/response_improvements_example.py +144 -144
  202. tests/scrapy_comparison/ofweek_scrapy.py +138 -138
  203. tests/scrapy_comparison/scrapy_test.py +133 -133
  204. tests/simple_command_test.py +119 -119
  205. tests/simple_crawlo_test.py +127 -127
  206. tests/simple_log_test.py +57 -57
  207. tests/simple_log_test2.py +137 -137
  208. tests/simple_optimization_test.py +128 -128
  209. tests/simple_queue_type_test.py +41 -41
  210. tests/simple_spider_test.py +49 -49
  211. tests/simple_test.py +47 -47
  212. tests/spider_log_timing_test.py +177 -177
  213. tests/test_advanced_tools.py +148 -148
  214. tests/test_all_commands.py +230 -230
  215. tests/test_all_pipeline_fingerprints.py +133 -133
  216. tests/test_all_redis_key_configs.py +145 -145
  217. tests/test_authenticated_proxy.py +141 -141
  218. tests/test_batch_processor.py +178 -178
  219. tests/test_cleaners.py +54 -54
  220. tests/test_component_factory.py +174 -174
  221. tests/test_comprehensive.py +146 -146
  222. tests/test_config_consistency.py +80 -80
  223. tests/test_config_merge.py +152 -152
  224. tests/test_config_validator.py +182 -182
  225. tests/test_controlled_spider_mixin.py +79 -79
  226. tests/test_crawlo_proxy_integration.py +108 -108
  227. tests/test_date_tools.py +123 -123
  228. tests/test_dedup_fix.py +220 -220
  229. tests/test_dedup_pipeline_consistency.py +125 -0
  230. tests/test_default_header_middleware.py +313 -313
  231. tests/test_distributed.py +65 -65
  232. tests/test_double_crawlo_fix.py +204 -204
  233. tests/test_double_crawlo_fix_simple.py +124 -124
  234. tests/test_download_delay_middleware.py +221 -221
  235. tests/test_downloader_proxy_compatibility.py +268 -268
  236. tests/test_dynamic_downloaders_proxy.py +124 -124
  237. tests/test_dynamic_proxy.py +92 -92
  238. tests/test_dynamic_proxy_config.py +146 -146
  239. tests/test_dynamic_proxy_real.py +109 -109
  240. tests/test_edge_cases.py +303 -303
  241. tests/test_enhanced_error_handler.py +270 -270
  242. tests/test_enhanced_error_handler_comprehensive.py +245 -245
  243. tests/test_env_config.py +121 -121
  244. tests/test_error_handler_compatibility.py +112 -112
  245. tests/test_factories.py +252 -252
  246. tests/test_final_validation.py +153 -153
  247. tests/test_fingerprint_consistency.py +135 -135
  248. tests/test_fingerprint_simple.py +51 -51
  249. tests/test_framework_env_usage.py +103 -103
  250. tests/test_framework_logger.py +66 -66
  251. tests/test_framework_startup.py +64 -64
  252. tests/test_get_component_logger.py +83 -83
  253. tests/test_hash_performance.py +99 -99
  254. tests/test_integration.py +169 -169
  255. tests/test_item_dedup_redis_key.py +122 -122
  256. tests/test_large_scale_config.py +112 -112
  257. tests/test_large_scale_helper.py +235 -235
  258. tests/test_logging_enhancements.py +375 -0
  259. tests/test_logging_final.py +185 -0
  260. tests/test_logging_integration.py +313 -0
  261. tests/test_logging_system.py +282 -282
  262. tests/test_middleware_debug.py +142 -0
  263. tests/test_mode_change.py +72 -72
  264. tests/test_mode_consistency.py +51 -51
  265. tests/test_offsite_middleware.py +244 -244
  266. tests/test_offsite_middleware_simple.py +203 -203
  267. tests/test_parsel.py +29 -29
  268. tests/test_performance.py +327 -327
  269. tests/test_performance_monitor.py +115 -115
  270. tests/test_pipeline_fingerprint_consistency.py +86 -86
  271. tests/test_priority_behavior.py +212 -0
  272. tests/test_priority_consistency.py +152 -0
  273. tests/test_priority_consistency_fixed.py +250 -0
  274. tests/test_proxy_api.py +264 -264
  275. tests/test_proxy_health_check.py +32 -32
  276. tests/test_proxy_middleware.py +121 -121
  277. tests/test_proxy_middleware_enhanced.py +216 -216
  278. tests/test_proxy_middleware_integration.py +136 -136
  279. tests/test_proxy_middleware_refactored.py +184 -184
  280. tests/test_proxy_providers.py +56 -56
  281. tests/test_proxy_stats.py +19 -19
  282. tests/test_proxy_strategies.py +59 -59
  283. tests/test_queue_empty_check.py +41 -41
  284. tests/test_queue_manager_double_crawlo.py +173 -173
  285. tests/test_queue_manager_redis_key.py +179 -179
  286. tests/test_queue_naming.py +154 -154
  287. tests/test_queue_type.py +106 -106
  288. tests/test_queue_type_redis_config_consistency.py +131 -0
  289. tests/test_random_headers_default.py +323 -0
  290. tests/test_random_headers_necessity.py +309 -0
  291. tests/test_random_user_agent.py +72 -72
  292. tests/test_real_scenario_proxy.py +195 -195
  293. tests/test_redis_config.py +28 -28
  294. tests/test_redis_connection_pool.py +294 -294
  295. tests/test_redis_key_naming.py +181 -181
  296. tests/test_redis_key_validator.py +123 -123
  297. tests/test_redis_queue.py +224 -224
  298. tests/test_redis_queue_name_fix.py +175 -175
  299. tests/test_redis_queue_type_fallback.py +130 -0
  300. tests/test_request_ignore_middleware.py +182 -182
  301. tests/test_request_params.py +111 -111
  302. tests/test_request_serialization.py +70 -70
  303. tests/test_response_code_middleware.py +349 -349
  304. tests/test_response_filter_middleware.py +427 -427
  305. tests/test_response_improvements.py +152 -152
  306. tests/test_retry_middleware.py +334 -242
  307. tests/test_retry_middleware_realistic.py +274 -0
  308. tests/test_scheduler.py +252 -252
  309. tests/test_scheduler_config_update.py +133 -133
  310. tests/test_simple_response.py +61 -61
  311. tests/test_telecom_spider_redis_key.py +205 -205
  312. tests/test_template_content.py +87 -87
  313. tests/test_template_redis_key.py +134 -134
  314. tests/test_tools.py +159 -159
  315. tests/test_user_agent_randomness.py +177 -0
  316. tests/test_user_agents.py +96 -96
  317. tests/tools_example.py +260 -260
  318. tests/untested_features_report.md +138 -138
  319. tests/verify_debug.py +51 -51
  320. tests/verify_distributed.py +117 -117
  321. tests/verify_log_fix.py +111 -111
  322. crawlo-1.4.1.dist-info/METADATA +0 -1199
  323. crawlo-1.4.1.dist-info/RECORD +0 -309
  324. {crawlo-1.4.1.dist-info → crawlo-1.4.3.dist-info}/WHEEL +0 -0
  325. {crawlo-1.4.1.dist-info → crawlo-1.4.3.dist-info}/entry_points.txt +0 -0
  326. {crawlo-1.4.1.dist-info → crawlo-1.4.3.dist-info}/top_level.txt +0 -0
@@ -1,1199 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: crawlo
3
- Version: 1.4.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-%3C%3D3.12-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
- # 检查所有爬虫
264
- crawlo check
265
-
266
- # 检查特定爬虫
267
- crawlo check myspider
268
- ```
269
-
270
- ### crawlo stats
271
-
272
- 查看爬虫统计信息。
273
-
274
- ```bash
275
- # 查看统计信息
276
- crawlo stats
277
- ```
278
-
279
- ---
280
-
281
- <!-- 配置方式 section -->
282
- <h2 align="center">⚙️ 配置方式</h2>
283
-
284
- Crawlo 提供了多种灵活的配置方式,以适应不同的使用场景和开发需求。
285
-
286
- ### 三种配置方式详解
287
-
288
- #### 1. 配置工厂方式(推荐)
289
-
290
- 使用 `CrawloConfig` 配置工厂是推荐的配置方式,它提供了类型安全和智能提示。
291
-
292
- ``python
293
- from crawlo.config import CrawloConfig
294
- from crawlo.crawler import CrawlerProcess
295
-
296
- # 单机模式配置
297
- config = CrawloConfig.standalone(
298
- concurrency=8,
299
- download_delay=1.0
300
- )
301
-
302
- # 分布式模式配置
303
- config = CrawloConfig.distributed(
304
- redis_host='127.0.0.1',
305
- redis_port=6379,
306
- project_name='myproject',
307
- concurrency=16
308
- )
309
-
310
- # 自动检测模式配置
311
- config = CrawloConfig.auto(concurrency=12)
312
-
313
- # 从环境变量读取配置
314
- config = CrawloConfig.from_env()
315
-
316
- # 创建爬虫进程
317
- process = CrawlerProcess(settings=config.to_dict())
318
- ```
319
-
320
- #### 2. 直接配置方式
321
-
322
- 直接在 `settings.py` 文件中配置各项参数,适合需要精细控制的场景。
323
-
324
- ```
325
- # settings.py
326
- PROJECT_NAME = 'myproject'
327
- RUN_MODE = 'standalone' # 或 'distributed' 或 'auto'
328
- CONCURRENCY = 8
329
- DOWNLOAD_DELAY = 1.0
330
-
331
- # 分布式模式下需要配置Redis
332
- REDIS_HOST = '127.0.0.1'
333
- REDIS_PORT = 6379
334
- REDIS_PASSWORD = ''
335
-
336
- # 其他配置...
337
- ```
338
-
339
- #### 3. 环境变量方式
340
-
341
- 通过环境变量配置,适合部署和CI/CD场景。
342
-
343
- ```bash
344
- # 设置环境变量
345
- export CRAWLO_MODE=standalone
346
- export CONCURRENCY=8
347
- export DOWNLOAD_DELAY=1.0
348
- export REDIS_HOST=127.0.0.1
349
- export REDIS_PORT=6379
350
- ```
351
-
352
- ```python
353
- # 在代码中读取环境变量
354
- from crawlo.config import CrawloConfig
355
- config = CrawloConfig.from_env()
356
- process = CrawlerProcess(settings=config.to_dict())
357
- ```
358
-
359
- ### 不同运行模式下的最佳配置方式
360
-
361
- #### 单机模式 (standalone)
362
-
363
- 适用于开发调试、小规模数据采集、个人项目。
364
-
365
- **推荐配置方式:**
366
- ``python
367
- from crawlo.config import CrawloConfig
368
- config = CrawloConfig.standalone(concurrency=4, download_delay=1.0)
369
- process = CrawlerProcess(settings=config.to_dict())
370
- ```
371
-
372
- **特点:**
373
- - 简单易用,资源占用少
374
- - 无需额外依赖(如Redis)
375
- - 适合个人开发环境
376
-
377
- #### 分布式模式 (distributed)
378
-
379
- 适用于大规模数据采集、多节点协同工作、高并发需求。
380
-
381
- **推荐配置方式:**
382
- ``python
383
- from crawlo.config import CrawloConfig
384
- config = CrawloConfig.distributed(
385
- redis_host='your_redis_host',
386
- redis_port=6379,
387
- project_name='myproject',
388
- concurrency=16
389
- )
390
- process = CrawlerProcess(settings=config.to_dict())
391
- ```
392
-
393
- **特点:**
394
- - 支持多节点扩展
395
- - 高并发处理能力
396
- - 需要Redis支持
397
-
398
- #### 自动检测模式 (auto)
399
-
400
- 适用于希望根据环境自动选择最佳运行方式。
401
-
402
- **推荐配置方式:**
403
- ``python
404
- from crawlo.config import CrawloConfig
405
- config = CrawloConfig.auto(concurrency=12)
406
- process = CrawlerProcess(settings=config.to_dict())
407
- ```
408
-
409
- **特点:**
410
- - 智能检测环境配置
411
- - 自动选择运行模式
412
- - 适合在不同环境中使用同一套配置
413
-
414
- ### 组件配置说明
415
-
416
- Crawlo框架的中间件、管道和扩展组件采用模块化设计,框架会自动加载默认组件,用户只需配置自定义组件。
417
-
418
- #### 中间件配置
419
-
420
- 框架默认加载以下中间件:
421
- - RequestIgnoreMiddleware:忽略无效请求
422
- - DownloadDelayMiddleware:控制请求频率
423
- - DefaultHeaderMiddleware:添加默认请求头
424
- - ProxyMiddleware:设置代理
425
- - OffsiteMiddleware:站外请求过滤
426
- - RetryMiddleware:失败请求重试
427
- - ResponseCodeMiddleware:处理特殊状态码
428
- - ResponseFilterMiddleware:响应内容过滤
429
-
430
- 用户可以通过`CUSTOM_MIDDLEWARES`配置自定义中间件:
431
-
432
- ``python
433
- # settings.py
434
- CUSTOM_MIDDLEWARES = [
435
- 'myproject.middlewares.CustomMiddleware',
436
- ]
437
- ```
438
-
439
- > **注意**:DefaultHeaderMiddleware 和 OffsiteMiddleware 需要相应的配置才能启用:
440
- > - DefaultHeaderMiddleware 需要配置 `DEFAULT_REQUEST_HEADERS` 或 `USER_AGENT` 参数
441
- > - OffsiteMiddleware 需要配置 `ALLOWED_DOMAINS` 参数
442
- >
443
- > 如果未配置相应参数,这些中间件会因为 NotConfiguredError 而被禁用。
444
-
445
- > **注意**:中间件的顺序很重要。SimpleProxyMiddleware 通常放在列表末尾,
446
- > 这样可以在所有默认中间件处理后再应用代理设置。
447
-
448
- #### 管道配置
449
-
450
- 框架默认加载以下管道:
451
- - ConsolePipeline:控制台输出
452
- - 默认去重管道(根据运行模式自动选择)
453
-
454
- 用户可以通过`CUSTOM_PIPELINES`配置自定义管道:
455
-
456
- ``python
457
- # settings.py
458
- CUSTOM_PIPELINES = [
459
- 'crawlo.pipelines.json_pipeline.JsonPipeline',
460
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline',
461
- ]
462
- ```
463
-
464
- #### 扩展配置
465
-
466
- 框架默认加载以下扩展:
467
- - LogIntervalExtension:定时日志
468
- - LogStats:统计信息
469
- - CustomLoggerExtension:自定义日志
470
-
471
- 用户可以通过`CUSTOM_EXTENSIONS`配置自定义扩展:
472
-
473
- ```python
474
- # settings.py
475
- CUSTOM_EXTENSIONS = [
476
- 'crawlo.extension.memory_monitor.MemoryMonitorExtension',
477
- ]
478
- ```
479
-
480
- <!-- 架构设计 section -->
481
- <h2 align="center">🏗️ 架构设计</h2>
482
-
483
- ### 核心组件说明
484
-
485
- Crawlo 框架由以下核心组件构成:
486
-
487
- <table>
488
- <thead>
489
- <tr>
490
- <th>组件</th>
491
- <th>功能描述</th>
492
- </tr>
493
- </thead>
494
- <tbody>
495
- <tr>
496
- <td><strong>Crawler</strong></td>
497
- <td>爬虫运行实例,管理Spider与引擎的生命周期</td>
498
- </tr>
499
- <tr>
500
- <td><strong>Engine</strong></td>
501
- <td>引擎组件,协调Scheduler、Downloader、Processor</td>
502
- </tr>
503
- <tr>
504
- <td><strong>Scheduler</strong></td>
505
- <td>调度器,管理请求队列和去重过滤</td>
506
- </tr>
507
- <tr>
508
- <td><strong>Downloader</strong></td>
509
- <td>下载器,负责网络请求,支持多种实现(aiohttp, httpx, curl-cffi)</td>
510
- </tr>
511
- <tr>
512
- <td><strong>Processor</strong></td>
513
- <td>处理器,处理响应数据和管道</td>
514
- </tr>
515
- <tr>
516
- <td><strong>QueueManager</strong></td>
517
- <td>统一的队列管理器,支持内存队列和Redis队列的自动切换</td>
518
- </tr>
519
- <tr>
520
- <td><strong>Filter</strong></td>
521
- <td>请求去重过滤器,支持内存和Redis两种实现</td>
522
- </tr>
523
- <tr>
524
- <td><strong>Middleware</strong></td>
525
- <td>中间件系统,处理请求/响应的预处理和后处理</td>
526
- </tr>
527
- <tr>
528
- <td><strong>Pipeline</strong></td>
529
- <td>数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能</td>
530
- </tr>
531
- <tr>
532
- <td><strong>Spider</strong></td>
533
- <td>爬虫基类,定义爬取逻辑</td>
534
- </tr>
535
- </tbody>
536
- </table>
537
-
538
- ### 运行模式
539
-
540
- Crawlo支持三种运行模式:
541
-
542
- <table>
543
- <thead>
544
- <tr>
545
- <th>模式</th>
546
- <th>描述</th>
547
- <th>队列类型</th>
548
- <th>过滤器类型</th>
549
- </tr>
550
- </thead>
551
- <tbody>
552
- <tr>
553
- <td><strong>standalone</strong></td>
554
- <td>单机模式</td>
555
- <td>内存队列</td>
556
- <td>内存过滤器</td>
557
- </tr>
558
- <tr>
559
- <td><strong>distributed</strong></td>
560
- <td>分布式模式</td>
561
- <td>Redis队列</td>
562
- <td>Redis过滤器</td>
563
- </tr>
564
- <tr>
565
- <td><strong>auto</strong></td>
566
- <td>自动检测模式</td>
567
- <td>根据环境自动选择最佳运行方式</td>
568
- <td>根据环境自动选择</td>
569
- </tr>
570
- </tbody>
571
- </table>
572
-
573
- > **运行模式说明**: distributed模式为多节点分布式设计,强制使用Redis队列和去重;standalone+auto为单机智能模式,根据环境自动选择内存或Redis队列与去重策略,零配置启动。
574
-
575
- #### 运行模式选择指南
576
-
577
- ##### 1. 单机模式 (standalone)
578
- - **适用场景**:
579
- - 开发和测试阶段
580
- - 小规模数据采集(几千到几万条数据)
581
- - 学习和演示用途
582
- - 对目标网站负载要求不高的场景
583
- - **优势**:
584
- - 配置简单,无需额外依赖
585
- - 资源消耗低
586
- - 启动快速
587
- - 适合本地开发调试
588
- - **限制**:
589
- - 无法跨会话去重
590
- - 无法分布式部署
591
- - 内存占用随数据量增长
592
-
593
- ##### 2. 分布式模式 (distributed)
594
- - **适用场景**:
595
- - 大规模数据采集(百万级以上)
596
- - 需要多节点协同工作
597
- - 要求跨会话、跨节点去重
598
- - 生产环境部署
599
- - **优势**:
600
- - 支持水平扩展
601
- - 跨节点任务协调
602
- - 持久化去重过滤
603
- - 高可用性
604
- - **要求**:
605
- - 需要Redis服务器
606
- - 网络环境稳定
607
- - 更复杂的配置管理
608
-
609
- ##### 3. 自动模式 (auto)
610
- - **适用场景**:
611
- - 希望根据环境自动选择最佳配置
612
- - 开发和生产环境使用同一套代码
613
- - 动态适应运行环境
614
- - **工作机制**:
615
- - 检测Redis可用性
616
- - Redis可用时自动切换到分布式模式
617
- - Redis不可用时回退到单机模式
618
- - **优势**:
619
- - 环境适应性强
620
- - 部署灵活
621
- - 开发和生产环境配置统一
622
-
623
- #### 队列类型选择指南
624
-
625
- Crawlo支持三种队列类型,可通过`QUEUE_TYPE`配置项设置:
626
-
627
- - **memory**:使用内存队列,适用于单机模式
628
- - **redis**:使用Redis队列,适用于分布式模式
629
- - **auto**:自动检测模式,根据Redis可用性自动选择
630
-
631
- 推荐使用`auto`模式,让框架根据环境自动选择最适合的队列类型。
632
-
633
- #### Redis Key 命名规范
634
-
635
- 在分布式模式下,Crawlo框架使用Redis作为队列和去重存储。为了确保不同项目和爬虫之间的数据隔离,框架采用统一的Redis Key命名规范:
636
-
637
- ##### 默认命名规则
638
- Redis Key遵循以下命名格式:`crawlo:{PROJECT_NAME}:{component}:{identifier}`
639
-
640
- 其中:
641
- - `PROJECT_NAME`:项目名称,用于区分不同项目
642
- - `component`:组件类型,如`queue`、`filter`、`item`
643
- - `identifier`:具体标识符,如`requests`、`processing`、`failed`、`fingerprint`
644
-
645
- ##### 具体Key格式
646
- 1. **请求队列**:`crawlo:{PROJECT_NAME}:queue:requests`
647
- - 用于存储待处理的请求任务
648
-
649
- 2. **处理中队列**:`crawlo:{PROJECT_NAME}:queue:processing`
650
- - 用于存储正在处理的请求任务
651
-
652
- 3. **失败队列**:`crawlo:{PROJECT_NAME}:queue:failed`
653
- - 用于存储处理失败的请求任务
654
-
655
- 4. **请求去重**:`crawlo:{PROJECT_NAME}:filter:fingerprint`
656
- - 用于存储请求URL的指纹,实现去重功能
657
-
658
- 5. **数据项去重**:`crawlo:{PROJECT_NAME}:item:fingerprint`
659
- - 用于存储数据项的指纹,防止重复存储
660
-
661
- ##### 自定义队列名称
662
- 用户可以通过`SCHEDULER_QUEUE_NAME`配置项自定义请求队列名称。处理中队列和失败队列会基于请求队列名称自动生成:
663
- - 处理中队列:将`:queue:requests`替换为`:queue:processing`
664
- - 失败队列:将`:queue:requests`替换为`:queue:failed`
665
-
666
- 示例配置:
667
- ```python
668
- # settings.py
669
- SCHEDULER_QUEUE_NAME = f'crawlo:{PROJECT_NAME}:queue:requests'
670
- ```
671
-
672
- ##### 命名规范优势
673
- 1. **命名空间隔离**:通过项目名称实现不同项目间的数据隔离
674
- 2. **组件分类清晰**:通过组件类型区分不同功能模块
675
- 3. **易于监控和管理**:统一的命名格式便于Redis监控和管理
676
- 4. **防止命名冲突**:避免不同项目或组件间的Key冲突
677
-
678
- <!-- 配置系统 section -->
679
- <h2 align="center">🎛️ 配置系统</h2>
680
-
681
- ### 传统配置方式
682
-
683
- ```
684
- # settings.py
685
- PROJECT_NAME = 'myproject'
686
- CONCURRENCY = 16
687
- DOWNLOAD_DELAY = 1.0
688
- QUEUE_TYPE = 'memory' # 单机模式
689
- # QUEUE_TYPE = 'redis' # 分布式模式
690
-
691
- # Redis 配置 (分布式模式下使用)
692
- REDIS_HOST = 'localhost'
693
- REDIS_PORT = 6379
694
- REDIS_DB = 0
695
- REDIS_PASSWORD = ''
696
-
697
- # 数据管道配置
698
- # 注意:框架默认管道已自动加载,此处仅用于添加自定义管道
699
- CUSTOM_PIPELINES = [
700
- 'crawlo.pipelines.json_pipeline.JsonPipeline',
701
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL存储管道
702
- ]
703
-
704
- # 高级日志配置
705
- LOG_FILE = 'logs/spider.log'
706
- LOG_LEVEL = 'INFO'
707
- LOG_MAX_BYTES = 10 * 1024 * 1024 # 10MB
708
- LOG_BACKUP_COUNT = 5
709
- LOG_JSON_FORMAT = False # 设置为True启用JSON格式
710
-
711
- # 启用高级日志扩展
712
- ADVANCED_LOGGING_ENABLED = True
713
-
714
- # 启用日志监控
715
- LOG_MONITOR_ENABLED = True
716
- LOG_MONITOR_INTERVAL = 30
717
- LOG_MONITOR_DETAILED_STATS = True
718
-
719
- # 添加扩展(注意:框架默认扩展已自动加载,此处仅用于添加自定义扩展)
720
- CUSTOM_EXTENSIONS = [
721
- 'crawlo.extension.memory_monitor.MemoryMonitorExtension',
722
- ]
723
- ```
724
-
725
- ### MySQL 管道配置
726
-
727
- Crawlo 提供了现成的 MySQL 管道实现,可以轻松将爬取的数据存储到 MySQL 数据库中:
728
-
729
- ```
730
- # 在 settings.py 中启用 MySQL 管道
731
- CUSTOM_PIPELINES = [
732
- 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline',
733
- ]
734
-
735
- # MySQL 数据库配置
736
- MYSQL_HOST = 'localhost'
737
- MYSQL_PORT = 3306
738
- MYSQL_USER = 'your_username'
739
- MYSQL_PASSWORD = 'your_password'
740
- MYSQL_DB = 'your_database'
741
- MYSQL_TABLE = 'your_table_name'
742
-
743
- # 可选的批量插入配置
744
- MYSQL_BATCH_SIZE = 100
745
- MYSQL_USE_BATCH = True
746
- ```
747
-
748
- MySQL 管道特性:
749
- - **异步操作**:基于 asyncmy 驱动,提供高性能的异步数据库操作
750
- - **连接池**:自动管理数据库连接,提高效率
751
- - **批量插入**:支持批量插入以提高性能
752
- - **事务支持**:确保数据一致性
753
- - **灵活配置**:支持自定义表名、批量大小等参数
754
-
755
- ### 命令行配置
756
-
757
- ```
758
- # 运行单个爬虫
759
- crawlo run myspider
760
-
761
- # 运行所有爬虫
762
- crawlo run all
763
-
764
- # 在项目子目录中也能正确运行
765
- cd subdirectory
766
- crawlo run myspider
767
- ```
768
-
769
- ---
770
-
771
- <!-- 核心组件 section -->
772
- <h2 align="center">🧩 核心组件</h2>
773
-
774
- ### Request类
775
-
776
- Request类是Crawlo框架中用于封装HTTP请求的核心组件,提供了丰富的功能来处理各种类型的HTTP请求。
777
-
778
- #### 基本用法
779
-
780
- ```python
781
- from crawlo import Request
782
-
783
- # 创建一个基本的GET请求
784
- request = Request('https://example.com')
785
-
786
- # 创建带回调函数的请求
787
- request = Request('https://example.com', callback=self.parse)
788
- ```
789
-
790
- #### params参数(GET请求参数)
791
-
792
- 使用`params`参数来添加GET请求的查询参数,这些参数会自动附加到URL上:
793
-
794
- ```python
795
- # GET请求带参数
796
- request = Request(
797
- url='https://httpbin.org/get',
798
- params={'key1': 'value1', 'key2': 'value2', 'page': 1},
799
- callback=self.parse
800
- )
801
- # 实际请求URL会变成: https://httpbin.org/get?key1=value1&key2=value2&page=1
802
-
803
- # 复杂参数示例
804
- request = Request(
805
- url='https://api.example.com/search',
806
- params={
807
- 'q': 'python爬虫',
808
- 'sort': 'date',
809
- 'order': 'desc',
810
- 'limit': 20
811
- },
812
- callback=self.parse_results
813
- )
814
- ```
815
-
816
- 对于GET请求,如果同时指定了[params](file:///Users/oscar/projects/Crawlo/crawlo/network/request.py#L55-L55)和[form_data](file:///Users/oscar/projects/Crawlo/crawlo/network/request.py#L53-L53)参数,它们都会被作为查询参数附加到URL上。
817
-
818
- #### form_data参数(POST表单数据)
819
-
820
- 使用`form_data`参数发送表单数据,会根据请求方法自动处理:
821
-
822
- ```python
823
- # POST请求发送表单数据
824
- request = Request(
825
- url='https://httpbin.org/post',
826
- method='POST',
827
- form_data={
828
- 'username': 'crawlo_user',
829
- 'password': 'secret_password',
830
- 'remember_me': 'true'
831
- },
832
- callback=self.parse_login
833
- )
834
-
835
- # GET请求使用form_data(会自动转换为查询参数)
836
- request = Request(
837
- url='https://httpbin.org/get',
838
- method='GET',
839
- form_data={
840
- 'search': 'crawlo framework',
841
- 'category': 'documentation'
842
- },
843
- callback=self.parse_search
844
- )
845
- ```
846
-
847
- 对于POST请求,[form_data](file:///Users/oscar/projects/Crawlo/crawlo/network/request.py#L53-L53)会被编码为`application/x-www-form-urlencoded`格式并作为请求体发送。
848
-
849
- #### json_body参数(JSON请求体)
850
-
851
- 使用`json_body`参数发送JSON数据:
852
-
853
- ```python
854
- # 发送JSON数据
855
- request = Request(
856
- url='https://api.example.com/users',
857
- method='POST',
858
- json_body={
859
- 'name': 'Crawlo User',
860
- 'email': 'user@example.com',
861
- 'preferences': {
862
- 'theme': 'dark',
863
- 'notifications': True
864
- }
865
- },
866
- callback=self.parse_response
867
- )
868
-
869
- # PUT请求更新资源
870
- request = Request(
871
- url='https://api.example.com/users/123',
872
- method='PUT',
873
- json_body={
874
- 'name': 'Updated Name',
875
- 'email': 'updated@example.com'
876
- },
877
- callback=self.parse_update
878
- )
879
- ```
880
-
881
- 使用[json_body](file:///Users/oscar/projects/Crawlo/crawlo/network/request.py#L54-L54)时,会自动设置`Content-Type: application/json`请求头,并将数据序列化为JSON格式。
882
-
883
- #### 混合使用参数
884
-
885
- 可以同时使用多种参数类型,框架会自动处理:
886
-
887
- ``python
888
- # GET请求同时使用params和form_data(都会作为查询参数)
889
- request = Request(
890
- url='https://api.example.com/search',
891
- params={'category': 'books'}, # 作为查询参数
892
- form_data={'q': 'python', 'limit': 10}, # 也作为查询参数
893
- callback=self.parse_search
894
- )
895
-
896
- # POST请求使用form_data和headers
897
- request = Request(
898
- url='https://api.example.com/upload',
899
- method='POST',
900
- form_data={'title': 'My Document'},
901
- headers={'Authorization': 'Bearer token123'},
902
- callback=self.parse_upload
903
- )
904
- ```
905
-
906
- #### 请求配置
907
-
908
- Request类支持丰富的配置选项:
909
-
910
- ```python
911
- request = Request(
912
- url='https://example.com',
913
- method='GET',
914
- headers={'User-Agent': 'Crawlo Bot'},
915
- cookies={'session_id': 'abc123'},
916
- priority=RequestPriority.HIGH,
917
- timeout=30,
918
- proxy='http://proxy.example.com:8080',
919
- dont_filter=True, # 跳过去重检查
920
- meta={'custom_key': 'custom_value'}, # 传递自定义元数据
921
- callback=self.parse
922
- )
923
- ```
924
-
925
- #### 链式调用
926
-
927
- Request类支持链式调用来简化配置:
928
-
929
- ``python
930
- request = Request('https://example.com')\
931
- .add_header('User-Agent', 'Crawlo Bot')\
932
- .set_proxy('http://proxy.example.com:8080')\
933
- .set_timeout(30)\
934
- .add_flag('important')\
935
- .set_meta('custom_key', 'custom_value')
936
- ```
937
-
938
- #### 优先级设置
939
-
940
- Crawlo提供了多种预定义的请求优先级:
941
-
942
- ``python
943
- from crawlo import Request, RequestPriority
944
-
945
- # 设置不同的优先级
946
- urgent_request = Request('https://example.com', priority=RequestPriority.URGENT)
947
- high_request = Request('https://example.com', priority=RequestPriority.HIGH)
948
- normal_request = Request('https://example.com', priority=RequestPriority.NORMAL)
949
- low_request = Request('https://example.com', priority=RequestPriority.LOW)
950
- background_request = Request('https://example.com', priority=RequestPriority.BACKGROUND)
951
- ```
952
-
953
- #### 动态加载器
954
-
955
- 对于需要JavaScript渲染的页面,可以启用动态加载器:
956
-
957
- ``python
958
- # 启用动态加载器
959
- request = Request('https://example.com')\
960
- .set_dynamic_loader(use_dynamic=True)
961
-
962
- # 或者使用链式调用
963
- request = Request('https://example.com')\
964
- .set_dynamic_loader(True, {'wait_time': 3, 'timeout': 30})
965
- ```
966
-
967
- ### 中间件系统
968
- 灵活的中间件系统,支持请求预处理、响应处理和异常处理。
969
-
970
- Crawlo框架内置了多种中间件,其中代理中间件有两种实现:
971
-
972
- 1. **ProxyMiddleware(复杂版)**:
973
- - 动态从API获取代理
974
- - 代理池管理
975
- - 健康检查和成功率统计
976
- - 复杂的代理提取逻辑
977
- - 适用于需要高级代理管理功能的场景
978
-
979
- 2. **SimpleProxyMiddleware(简化版)**:
980
- - 基于固定代理列表的简单实现
981
- - 轻量级,代码简洁
982
- - 易于配置和使用
983
- - 适用于只需要基本代理功能的场景
984
-
985
- 如果需要使用简化版代理中间件,可以在配置文件中替换默认的代理中间件:
986
-
987
- ``python
988
- # settings.py
989
- MIDDLEWARES = [
990
- # 注释掉复杂版代理中间件
991
- # 'crawlo.middleware.proxy.ProxyMiddleware',
992
- # 启用简化版代理中间件
993
- 'crawlo.middleware.simple_proxy.SimpleProxyMiddleware',
994
- ]
995
-
996
- # 配置代理列表
997
- PROXY_ENABLED = True
998
- PROXY_LIST = [
999
- "http://proxy1.example.com:8080",
1000
- "http://proxy2.example.com:8080",
1001
- ]
1002
- ```
1003
-
1004
- 有关代理中间件的详细使用说明,请参考[代理中间件示例项目](examples/simple_proxy_example/)。
1005
-
1006
- ### 管道系统
1007
- 可扩展的数据处理管道,支持多种存储方式(控制台、数据库等)和去重功能:
1008
- - **ConsolePipeline**: 控制台输出管道
1009
- - **JsonPipeline**: JSON文件存储管道
1010
- - **RedisDedupPipeline**: Redis去重管道,基于Redis集合实现分布式去重
1011
- - **AsyncmyMySQLPipeline**: MySQL数据库存储管道,基于asyncmy驱动
1012
-
1013
- ### 扩展组件
1014
- 功能增强扩展,包括日志、监控、性能分析等:
1015
- - **LogIntervalExtension**: 定时日志扩展
1016
- - **LogStats**: 统计日志扩展
1017
- - **CustomLoggerExtension**: 自定义日志扩展
1018
- - **MemoryMonitorExtension**: 内存监控扩展(监控爬虫进程内存使用情况)
1019
- - **PerformanceProfilerExtension**: 性能分析扩展
1020
- - **HealthCheckExtension**: 健康检查扩展
1021
- - **RequestRecorderExtension**: 请求记录扩展
1022
-
1023
- ### 过滤系统
1024
- 智能去重过滤,支持多种去重策略(内存、Redis、Bloom Filter)。
1025
-
1026
- ---
1027
-
1028
- <!-- 高级工具 section -->
1029
- <h2 align="center">🛠️ 高级工具</h2>
1030
-
1031
- Crawlo 框架提供了一系列高级工具,帮助开发者更好地处理大规模爬虫任务和复杂场景。
1032
-
1033
- ### 1. 工厂模式相关模块
1034
-
1035
- **功能**:
1036
- - 组件创建和依赖注入
1037
- - 单例模式支持
1038
- - 统一的组件管理机制
1039
-
1040
- **使用场景**:
1041
- - 需要统一管理组件创建过程
1042
- - 需要依赖注入功能
1043
- - 需要单例组件实例
1044
-
1045
- ### 2. 批处理工具
1046
-
1047
- **功能**:
1048
- - 大规模数据处理
1049
- - 并发控制
1050
- - 内存使用优化
1051
-
1052
- **使用场景**:
1053
- - 处理大量数据项
1054
- - 需要控制并发数量
1055
- - 内存敏感的数据处理任务
1056
-
1057
- ### 3. 受控爬虫混入类
1058
-
1059
- **功能**:
1060
- - 控制大规模请求生成
1061
- - 防止内存溢出
1062
- - 动态并发控制
1063
-
1064
- **使用场景**:
1065
- - 需要生成大量请求的爬虫
1066
- - 内存受限的环境
1067
- - 需要精确控制并发的场景
1068
-
1069
- ### 4. 大规模配置工具
1070
-
1071
- **功能**:
1072
- - 针对不同场景的优化配置
1073
- - 简化配置过程
1074
- - 提高爬取效率和稳定性
1075
-
1076
- **配置类型**:
1077
- - **保守型**: 资源受限环境
1078
- - **平衡型**: 一般生产环境
1079
- - **激进型**: 高性能服务器
1080
- - **内存优化型**: 内存受限但要处理大量请求
1081
-
1082
- **使用场景**:
1083
- - 处理数万+请求的大规模爬取
1084
- - 不同性能环境的适配
1085
- - 快速配置优化
1086
-
1087
- ### 5. 大规模爬虫辅助工具
1088
-
1089
- **功能**:
1090
- - 批量数据处理
1091
- - 进度管理和断点续传
1092
- - 内存使用优化
1093
- - 多种数据源支持
1094
-
1095
- **组件**:
1096
- - **LargeScaleHelper**: 批量迭代大量数据
1097
- - **ProgressManager**: 进度管理
1098
- - **MemoryOptimizer**: 内存优化
1099
- - **DataSourceAdapter**: 数据源适配器
1100
-
1101
- **使用场景**:
1102
- - 处理数万+ URL的爬虫
1103
- - 需要断点续传的功能
1104
- - 内存敏感的大规模处理任务
1105
-
1106
- ### 6. 自动爬虫模块导入
1107
-
1108
- **功能**:
1109
- - 自动发现和导入爬虫模块
1110
- - 无需手动导入即可注册爬虫
1111
- - 智能扫描项目中的爬虫文件
1112
-
1113
- **使用方式**:
1114
- 框架会自动扫描指定的`spider_modules`路径,导入其中的所有爬虫模块并自动注册爬虫类。用户只需在创建`CrawlerProcess`时指定`spider_modules`参数:
1115
-
1116
- ```python
1117
- # 指定爬虫模块路径,框架会自动导入并注册所有爬虫
1118
- spider_modules = ['myproject.spiders']
1119
- process = CrawlerProcess(spider_modules=spider_modules)
1120
-
1121
- # 运行指定的爬虫(无需手动导入)
1122
- asyncio.run(process.crawl('my_spider_name'))
1123
- ```
1124
-
1125
- **优势**:
1126
- - 简化项目结构,减少样板代码
1127
- - 自动化管理爬虫注册过程
1128
- - 提高开发效率,降低出错概率
1129
- - 保持代码整洁和一致性
1130
-
1131
- 有关这些高级工具的详细使用方法和实际案例,请参考 [高级工具示例项目](examples/advanced_tools_example/)。
1132
-
1133
- <!-- 示例项目 section -->
1134
- <h2 align="center">📦 示例项目</h2>
1135
-
1136
- - [OFweek分布式爬虫](examples/ofweek_distributed/) - 复杂的分布式爬虫示例,包含Redis去重功能
1137
- - [OFweek独立爬虫](examples/ofweek_standalone/) - 独立运行的爬虫示例
1138
- - [OFweek混合模式爬虫](examples/ofweek_spider/) - 支持单机和分布式模式切换的爬虫示例
1139
- - [高级工具示例](examples/advanced_tools_example/) - 展示Crawlo框架中各种高级工具的使用方法,包括工厂模式、批处理工具、受控爬虫混入类、大规模配置工具和大规模爬虫辅助工具
1140
-
1141
- ---
1142
-
1143
- <!-- Redis键名修复说明 section -->
1144
- <h2 align="center">🔧 Redis键名修复说明</h2>
1145
-
1146
- 在早期版本中,Crawlo框架存在Redis队列键名生成的双重前缀问题。具体表现为:
1147
-
1148
- - **问题现象**:Redis队列键名出现双重"crawlo"前缀,如`crawlo:crawlo:queue:requests`而不是正确的`crawlo:{project_name}:queue:requests`
1149
- - **影响范围**:影响分布式模式下的请求队列、处理队列和失败队列的正确识别和使用
1150
- - **根本原因**:队列管理器中的项目名称提取逻辑未能正确处理不同格式的队列名称
1151
-
1152
- **修复内容**:
1153
-
1154
- 1. **队列管理器优化**:
1155
- - 改进了[QueueConfig.from_settings](file:///Users/oscar/projects/Crawlo/crawlo/queue/queue_manager.py#L148-L180)方法,使其在`SCHEDULER_QUEUE_NAME`未设置时能正确使用基于项目名称的默认值
1156
- - 修复了队列管理器中从队列名称提取项目名称的逻辑,确保能正确处理各种前缀情况
1157
-
1158
- 2. **Redis队列实现改进**:
1159
- - 在[RedisPriorityQueue](file:///Users/oscar/projects/Crawlo/crawlo/queue/redis_priority_queue.py#L39-L76)中添加了`_normalize_queue_name`方法来规范化队列名称
1160
- - 处理了多重"crawlo"前缀的情况,确保队列名称符合统一规范
1161
-
1162
- 3. **配置文件调整**:
1163
- - 将`SCHEDULER_QUEUE_NAME`设置为注释状态,提供更大的配置灵活性
1164
- - 在所有模板和示例项目的配置文件中保持了一致性
1165
-
1166
- **验证测试**:
1167
- 通过专门的测试脚本验证了修复效果,确保在各种队列命名情况下都能正确生成和识别Redis键名。
1168
-
1169
- ---
1170
-
1171
- <!-- 文档 section -->
1172
- <h2 align="center">📚 文档</h2>
1173
-
1174
- 完整的文档请访问 [Crawlo Documentation](https://crawlo.readthedocs.io/)
1175
-
1176
- - [快速开始指南](docs/modules/index.md)
1177
- - [模块化文档](docs/modules/index.md)
1178
- - [核心引擎文档](docs/modules/core/engine.md)
1179
- - [调度器文档](docs/modules/core/scheduler.md)
1180
- - [下载器文档](docs/modules/downloader/index.md)
1181
- - [中间件文档](docs/modules/middleware/index.md)
1182
- - [管道文档](docs/modules/pipeline/index.md)
1183
- - [队列文档](docs/modules/queue/index.md)
1184
- - [过滤器文档](docs/modules/filter/index.md)
1185
- - [扩展组件文档](docs/modules/extension/index.md)
1186
-
1187
- ---
1188
-
1189
- <!-- 贡献 section -->
1190
- <h2 align="center">🤝 贡献</h2>
1191
-
1192
- 欢迎提交 Issue 和 Pull Request 来帮助改进 Crawlo!
1193
-
1194
- ---
1195
-
1196
- <!-- 许可证 section -->
1197
- <h2 align="center">📄 许可证</h2>
1198
-
1199
- 本项目采用 MIT 许可证,详情请见 [LICENSE](LICENSE) 文件。