crawlo 1.1.4__py3-none-any.whl → 1.1.5__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 (186) hide show
  1. crawlo/__init__.py +61 -34
  2. crawlo/__version__.py +1 -1
  3. crawlo/cleaners/__init__.py +61 -0
  4. crawlo/cleaners/data_formatter.py +226 -0
  5. crawlo/cleaners/encoding_converter.py +126 -0
  6. crawlo/cleaners/text_cleaner.py +233 -0
  7. crawlo/cli.py +40 -40
  8. crawlo/commands/__init__.py +13 -13
  9. crawlo/commands/check.py +594 -594
  10. crawlo/commands/genspider.py +151 -151
  11. crawlo/commands/list.py +155 -155
  12. crawlo/commands/run.py +285 -285
  13. crawlo/commands/startproject.py +300 -196
  14. crawlo/commands/stats.py +188 -188
  15. crawlo/commands/utils.py +186 -186
  16. crawlo/config.py +309 -279
  17. crawlo/config_validator.py +253 -0
  18. crawlo/core/__init__.py +2 -2
  19. crawlo/core/engine.py +346 -172
  20. crawlo/core/processor.py +40 -40
  21. crawlo/core/scheduler.py +137 -166
  22. crawlo/crawler.py +1027 -1027
  23. crawlo/downloader/__init__.py +266 -242
  24. crawlo/downloader/aiohttp_downloader.py +220 -212
  25. crawlo/downloader/cffi_downloader.py +256 -251
  26. crawlo/downloader/httpx_downloader.py +259 -259
  27. crawlo/downloader/hybrid_downloader.py +214 -0
  28. crawlo/downloader/playwright_downloader.py +403 -0
  29. crawlo/downloader/selenium_downloader.py +473 -0
  30. crawlo/event.py +11 -11
  31. crawlo/exceptions.py +81 -81
  32. crawlo/extension/__init__.py +37 -37
  33. crawlo/extension/health_check.py +141 -141
  34. crawlo/extension/log_interval.py +57 -57
  35. crawlo/extension/log_stats.py +81 -81
  36. crawlo/extension/logging_extension.py +43 -43
  37. crawlo/extension/memory_monitor.py +104 -88
  38. crawlo/extension/performance_profiler.py +133 -117
  39. crawlo/extension/request_recorder.py +107 -107
  40. crawlo/filters/__init__.py +154 -154
  41. crawlo/filters/aioredis_filter.py +280 -242
  42. crawlo/filters/memory_filter.py +269 -269
  43. crawlo/items/__init__.py +23 -23
  44. crawlo/items/base.py +21 -21
  45. crawlo/items/fields.py +53 -53
  46. crawlo/items/items.py +104 -104
  47. crawlo/middleware/__init__.py +21 -21
  48. crawlo/middleware/default_header.py +32 -32
  49. crawlo/middleware/download_delay.py +28 -28
  50. crawlo/middleware/middleware_manager.py +135 -135
  51. crawlo/middleware/proxy.py +272 -248
  52. crawlo/middleware/request_ignore.py +30 -30
  53. crawlo/middleware/response_code.py +18 -18
  54. crawlo/middleware/response_filter.py +26 -26
  55. crawlo/middleware/retry.py +124 -124
  56. crawlo/mode_manager.py +206 -201
  57. crawlo/network/__init__.py +21 -21
  58. crawlo/network/request.py +338 -311
  59. crawlo/network/response.py +360 -271
  60. crawlo/pipelines/__init__.py +21 -21
  61. crawlo/pipelines/bloom_dedup_pipeline.py +156 -156
  62. crawlo/pipelines/console_pipeline.py +39 -39
  63. crawlo/pipelines/csv_pipeline.py +316 -316
  64. crawlo/pipelines/database_dedup_pipeline.py +224 -224
  65. crawlo/pipelines/json_pipeline.py +218 -218
  66. crawlo/pipelines/memory_dedup_pipeline.py +115 -115
  67. crawlo/pipelines/mongo_pipeline.py +131 -131
  68. crawlo/pipelines/mysql_pipeline.py +316 -316
  69. crawlo/pipelines/pipeline_manager.py +56 -56
  70. crawlo/pipelines/redis_dedup_pipeline.py +166 -162
  71. crawlo/project.py +153 -153
  72. crawlo/queue/pqueue.py +37 -37
  73. crawlo/queue/queue_manager.py +320 -307
  74. crawlo/queue/redis_priority_queue.py +277 -209
  75. crawlo/settings/__init__.py +7 -7
  76. crawlo/settings/default_settings.py +216 -278
  77. crawlo/settings/setting_manager.py +99 -99
  78. crawlo/spider/__init__.py +639 -639
  79. crawlo/stats_collector.py +59 -59
  80. crawlo/subscriber.py +130 -130
  81. crawlo/task_manager.py +30 -30
  82. crawlo/templates/crawlo.cfg.tmpl +10 -10
  83. crawlo/templates/project/__init__.py.tmpl +3 -3
  84. crawlo/templates/project/items.py.tmpl +17 -17
  85. crawlo/templates/project/middlewares.py.tmpl +110 -110
  86. crawlo/templates/project/pipelines.py.tmpl +97 -97
  87. crawlo/templates/project/run.py.tmpl +251 -251
  88. crawlo/templates/project/settings.py.tmpl +326 -279
  89. crawlo/templates/project/settings_distributed.py.tmpl +120 -0
  90. crawlo/templates/project/settings_gentle.py.tmpl +95 -0
  91. crawlo/templates/project/settings_high_performance.py.tmpl +152 -0
  92. crawlo/templates/project/settings_simple.py.tmpl +69 -0
  93. crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
  94. crawlo/templates/spider/spider.py.tmpl +141 -141
  95. crawlo/tools/__init__.py +183 -0
  96. crawlo/tools/anti_crawler.py +269 -0
  97. crawlo/tools/authenticated_proxy.py +241 -0
  98. crawlo/tools/data_validator.py +181 -0
  99. crawlo/tools/date_tools.py +36 -0
  100. crawlo/tools/distributed_coordinator.py +387 -0
  101. crawlo/tools/retry_mechanism.py +221 -0
  102. crawlo/tools/scenario_adapter.py +263 -0
  103. crawlo/utils/__init__.py +35 -7
  104. crawlo/utils/batch_processor.py +261 -0
  105. crawlo/utils/controlled_spider_mixin.py +439 -439
  106. crawlo/utils/date_tools.py +290 -233
  107. crawlo/utils/db_helper.py +343 -343
  108. crawlo/utils/enhanced_error_handler.py +360 -0
  109. crawlo/utils/env_config.py +106 -0
  110. crawlo/utils/error_handler.py +126 -0
  111. crawlo/utils/func_tools.py +82 -82
  112. crawlo/utils/large_scale_config.py +286 -286
  113. crawlo/utils/large_scale_helper.py +343 -343
  114. crawlo/utils/log.py +128 -128
  115. crawlo/utils/performance_monitor.py +285 -0
  116. crawlo/utils/queue_helper.py +175 -175
  117. crawlo/utils/redis_connection_pool.py +335 -0
  118. crawlo/utils/redis_key_validator.py +200 -0
  119. crawlo/utils/request.py +267 -267
  120. crawlo/utils/request_serializer.py +219 -219
  121. crawlo/utils/spider_loader.py +62 -62
  122. crawlo/utils/system.py +11 -11
  123. crawlo/utils/tools.py +4 -4
  124. crawlo/utils/url.py +39 -39
  125. {crawlo-1.1.4.dist-info → crawlo-1.1.5.dist-info}/METADATA +401 -403
  126. crawlo-1.1.5.dist-info/RECORD +185 -0
  127. examples/__init__.py +7 -7
  128. tests/__init__.py +7 -7
  129. tests/advanced_tools_example.py +276 -0
  130. tests/authenticated_proxy_example.py +237 -0
  131. tests/cleaners_example.py +161 -0
  132. tests/config_validation_demo.py +103 -0
  133. {examples → tests}/controlled_spider_example.py +205 -205
  134. tests/date_tools_example.py +181 -0
  135. tests/dynamic_loading_example.py +524 -0
  136. tests/dynamic_loading_test.py +105 -0
  137. tests/env_config_example.py +134 -0
  138. tests/error_handling_example.py +172 -0
  139. tests/redis_key_validation_demo.py +131 -0
  140. tests/response_improvements_example.py +145 -0
  141. tests/test_advanced_tools.py +149 -0
  142. tests/test_all_redis_key_configs.py +146 -0
  143. tests/test_authenticated_proxy.py +142 -0
  144. tests/test_cleaners.py +55 -0
  145. tests/test_comprehensive.py +147 -0
  146. tests/test_config_validator.py +194 -0
  147. tests/test_date_tools.py +124 -0
  148. tests/test_dynamic_downloaders_proxy.py +125 -0
  149. tests/test_dynamic_proxy.py +93 -0
  150. tests/test_dynamic_proxy_config.py +147 -0
  151. tests/test_dynamic_proxy_real.py +110 -0
  152. tests/test_edge_cases.py +304 -0
  153. tests/test_enhanced_error_handler.py +271 -0
  154. tests/test_env_config.py +122 -0
  155. tests/test_error_handler_compatibility.py +113 -0
  156. tests/test_final_validation.py +153 -153
  157. tests/test_framework_env_usage.py +104 -0
  158. tests/test_integration.py +357 -0
  159. tests/test_item_dedup_redis_key.py +123 -0
  160. tests/test_parsel.py +30 -0
  161. tests/test_performance.py +328 -0
  162. tests/test_proxy_health_check.py +32 -32
  163. tests/test_proxy_middleware_integration.py +136 -136
  164. tests/test_proxy_providers.py +56 -56
  165. tests/test_proxy_stats.py +19 -19
  166. tests/test_proxy_strategies.py +59 -59
  167. tests/test_queue_manager_redis_key.py +177 -0
  168. tests/test_redis_config.py +28 -28
  169. tests/test_redis_connection_pool.py +295 -0
  170. tests/test_redis_key_naming.py +182 -0
  171. tests/test_redis_key_validator.py +124 -0
  172. tests/test_redis_queue.py +224 -224
  173. tests/test_request_serialization.py +70 -70
  174. tests/test_response_improvements.py +153 -0
  175. tests/test_scheduler.py +241 -241
  176. tests/test_simple_response.py +62 -0
  177. tests/test_telecom_spider_redis_key.py +206 -0
  178. tests/test_template_content.py +88 -0
  179. tests/test_template_redis_key.py +135 -0
  180. tests/test_tools.py +154 -0
  181. tests/tools_example.py +258 -0
  182. crawlo/core/enhanced_engine.py +0 -190
  183. crawlo-1.1.4.dist-info/RECORD +0 -117
  184. {crawlo-1.1.4.dist-info → crawlo-1.1.5.dist-info}/WHEEL +0 -0
  185. {crawlo-1.1.4.dist-info → crawlo-1.1.5.dist-info}/entry_points.txt +0 -0
  186. {crawlo-1.1.4.dist-info → crawlo-1.1.5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ 环境变量配置工具使用示例
5
+ 展示如何在 Crawlo 项目中正确使用环境变量配置工具
6
+ """
7
+ import os
8
+ import sys
9
+
10
+ # 添加项目根目录到Python路径
11
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
12
+
13
+ from crawlo.utils.env_config import get_env_var, get_redis_config, get_runtime_config
14
+ from crawlo.settings.setting_manager import SettingManager
15
+ from crawlo.settings import default_settings
16
+
17
+
18
+ def example_basic_usage():
19
+ """基本使用示例"""
20
+ print("=== 基本环境变量使用示例 ===")
21
+
22
+ # 获取字符串环境变量
23
+ project_name = get_env_var('PROJECT_NAME', 'my_crawler', str)
24
+ print(f"项目名称: {project_name}")
25
+
26
+ # 获取整数环境变量
27
+ concurrency = get_env_var('CONCURRENCY', 8, int)
28
+ print(f"并发数: {concurrency}")
29
+
30
+ # 获取布尔环境变量
31
+ debug_mode = get_env_var('DEBUG_MODE', False, bool)
32
+ print(f"调试模式: {debug_mode}")
33
+
34
+
35
+ def example_redis_config():
36
+ """Redis配置示例"""
37
+ print("\n=== Redis配置示例 ===")
38
+
39
+ # 获取Redis配置
40
+ redis_config = get_redis_config()
41
+ print(f"Redis主机: {redis_config['REDIS_HOST']}")
42
+ print(f"Redis端口: {redis_config['REDIS_PORT']}")
43
+ print(f"Redis密码: {'*' * len(redis_config['REDIS_PASSWORD']) if redis_config['REDIS_PASSWORD'] else '无'}")
44
+ print(f"Redis数据库: {redis_config['REDIS_DB']}")
45
+
46
+ # 生成Redis URL
47
+ if redis_config['REDIS_PASSWORD']:
48
+ redis_url = f"redis://:{redis_config['REDIS_PASSWORD']}@{redis_config['REDIS_HOST']}:{redis_config['REDIS_PORT']}/{redis_config['REDIS_DB']}"
49
+ else:
50
+ redis_url = f"redis://{redis_config['REDIS_HOST']}:{redis_config['REDIS_PORT']}/{redis_config['REDIS_DB']}"
51
+
52
+ print(f"Redis URL: {redis_url}")
53
+
54
+
55
+ def example_runtime_config():
56
+ """运行时配置示例"""
57
+ print("\n=== 运行时配置示例 ===")
58
+
59
+ # 获取运行时配置
60
+ runtime_config = get_runtime_config()
61
+ print(f"运行模式: {runtime_config['CRAWLO_MODE']}")
62
+ print(f"项目名称: {runtime_config['PROJECT_NAME']}")
63
+ print(f"并发数: {runtime_config['CONCURRENCY']}")
64
+
65
+
66
+ def example_settings_integration():
67
+ """与Settings集成示例"""
68
+ print("\n=== 与Settings集成示例 ===")
69
+
70
+ # 创建设置管理器
71
+ settings = SettingManager()
72
+
73
+ # 更新Redis相关设置
74
+ redis_config = get_redis_config()
75
+ settings.set('REDIS_HOST', redis_config['REDIS_HOST'])
76
+ settings.set('REDIS_PORT', redis_config['REDIS_PORT'])
77
+ settings.set('REDIS_PASSWORD', redis_config['REDIS_PASSWORD'])
78
+ settings.set('REDIS_DB', redis_config['REDIS_DB'])
79
+
80
+ # 更新运行时设置
81
+ runtime_config = get_runtime_config()
82
+ settings.set('PROJECT_NAME', runtime_config['PROJECT_NAME'])
83
+ settings.set('RUN_MODE', runtime_config['CRAWLO_MODE'])
84
+ settings.set('CONCURRENCY', runtime_config['CONCURRENCY'])
85
+
86
+ # 显示一些关键设置
87
+ print(f"项目名称: {settings.get('PROJECT_NAME')}")
88
+ print(f"运行模式: {settings.get('RUN_MODE')}")
89
+ print(f"并发数: {settings.get_int('CONCURRENCY')}")
90
+ print(f"Redis主机: {settings.get('REDIS_HOST')}")
91
+ print(f"Redis端口: {settings.get_int('REDIS_PORT')}")
92
+
93
+
94
+ def example_env_setup():
95
+ """环境变量设置示例"""
96
+ print("\n=== 环境变量设置示例 ===")
97
+ print("在命令行中设置环境变量的示例:")
98
+ print(" Windows (PowerShell):")
99
+ print(" $env:PROJECT_NAME = \"my_distributed_crawler\"")
100
+ print(" $env:REDIS_HOST = \"redis.example.com\"")
101
+ print(" $env:REDIS_PORT = \"6380\"")
102
+ print(" $env:CONCURRENCY = \"16\"")
103
+ print(" $env:CRAWLO_MODE = \"distributed\"")
104
+ print()
105
+ print(" Linux/macOS:")
106
+ print(" export PROJECT_NAME=\"my_distributed_crawler\"")
107
+ print(" export REDIS_HOST=\"redis.example.com\"")
108
+ print(" export REDIS_PORT=\"6380\"")
109
+ print(" export CONCURRENCY=\"16\"")
110
+ print(" export CRAWLO_MODE=\"distributed\"")
111
+
112
+
113
+ if __name__ == '__main__':
114
+ # 设置一些测试环境变量
115
+ os.environ['PROJECT_NAME'] = 'test_crawler'
116
+ os.environ['CONCURRENCY'] = '12'
117
+ os.environ['DEBUG_MODE'] = 'true'
118
+ os.environ['REDIS_HOST'] = 'redis.test.com'
119
+ os.environ['REDIS_PORT'] = '6380'
120
+ os.environ['REDIS_PASSWORD'] = 'test_password'
121
+ os.environ['CRAWLO_MODE'] = 'distributed'
122
+
123
+ # 运行示例
124
+ example_basic_usage()
125
+ example_redis_config()
126
+ example_runtime_config()
127
+ example_settings_integration()
128
+ example_env_setup()
129
+
130
+ # 清理测试环境变量
131
+ for var in ['PROJECT_NAME', 'CONCURRENCY', 'DEBUG_MODE', 'REDIS_HOST',
132
+ 'REDIS_PORT', 'REDIS_PASSWORD', 'CRAWLO_MODE']:
133
+ if var in os.environ:
134
+ del os.environ[var]
@@ -0,0 +1,172 @@
1
+ #!/usr/bin/python
2
+ # -*- coding:UTF-8 -*-
3
+ """
4
+ 错误处理使用示例
5
+ 展示如何在实际项目中使用增强版错误处理工具
6
+ """
7
+ import sys
8
+ import os
9
+ import asyncio
10
+ import time
11
+ from typing import Optional
12
+
13
+ # 添加项目根目录到 Python 路径
14
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
15
+
16
+ from crawlo.utils.enhanced_error_handler import (
17
+ EnhancedErrorHandler,
18
+ ErrorContext,
19
+ DetailedException,
20
+ handle_exception
21
+ )
22
+
23
+
24
+ # 创建错误处理器实例
25
+ error_handler = EnhancedErrorHandler("example_module")
26
+
27
+
28
+ class DatabaseConnectionError(DetailedException):
29
+ """数据库连接错误"""
30
+ pass
31
+
32
+
33
+ class NetworkTimeoutError(DetailedException):
34
+ """网络超时错误"""
35
+ pass
36
+
37
+
38
+ @handle_exception(context="数据库连接", module="database", function="connect_to_db", error_code="DB001")
39
+ def connect_to_db(host: str, port: int) -> bool:
40
+ """模拟数据库连接"""
41
+ print(f"正在连接数据库 {host}:{port}...")
42
+
43
+ # 模拟连接失败
44
+ if host == "invalid.host":
45
+ raise DatabaseConnectionError(
46
+ "无法连接到数据库服务器",
47
+ context=ErrorContext(context="数据库连接失败", module="database", function="connect_to_db"),
48
+ error_code="DB001",
49
+ host=host,
50
+ port=port
51
+ )
52
+
53
+ # 模拟连接成功
54
+ print("✅ 数据库连接成功")
55
+ return True
56
+
57
+
58
+ @error_handler.retry_on_failure(max_retries=3, delay=0.5, backoff_factor=2.0)
59
+ async def fetch_data_from_api(url: str) -> dict:
60
+ """模拟从API获取数据(带重试机制)"""
61
+ print(f"正在从 {url} 获取数据...")
62
+
63
+ # 模拟网络问题
64
+ if url == "https://slow.api.com/data":
65
+ time.sleep(2) # 模拟慢速响应
66
+ raise NetworkTimeoutError(
67
+ "API响应超时",
68
+ context=ErrorContext(context="API调用超时", module="api", function="fetch_data_from_api"),
69
+ error_code="API001",
70
+ url=url,
71
+ timeout=1
72
+ )
73
+
74
+ if url == "https://error.api.com/data":
75
+ raise NetworkTimeoutError(
76
+ "API服务器错误",
77
+ context=ErrorContext(context="API服务器错误", module="api", function="fetch_data_from_api"),
78
+ error_code="API002",
79
+ url=url,
80
+ status_code=500
81
+ )
82
+
83
+ # 模拟成功响应
84
+ print("✅ API数据获取成功")
85
+ return {"data": "sample data", "status": "success"}
86
+
87
+
88
+ def process_data(data: dict) -> Optional[str]:
89
+ """处理数据"""
90
+ try:
91
+ print("正在处理数据...")
92
+
93
+ # 模拟处理错误
94
+ if not data:
95
+ raise ValueError("数据为空")
96
+
97
+ if "error" in data:
98
+ raise RuntimeError(f"数据处理失败: {data['error']}")
99
+
100
+ # 模拟处理成功
101
+ result = f"处理完成: {data.get('data', 'no data')}"
102
+ print(f"✅ {result}")
103
+ return result
104
+
105
+ except Exception as e:
106
+ # 使用错误处理器处理异常
107
+ context = ErrorContext(context="数据处理", module="data_processor", function="process_data")
108
+ error_handler.handle_error(e, context=context, raise_error=False)
109
+ return None
110
+
111
+
112
+ async def main():
113
+ """主函数"""
114
+ print("🚀 错误处理使用示例")
115
+ print("=" * 50)
116
+
117
+ # 1. 测试数据库连接(成功情况)
118
+ print("1. 测试数据库连接(成功情况)")
119
+ try:
120
+ connect_to_db("localhost", 5432)
121
+ except Exception as e:
122
+ print(f"❌ 意外错误: {e}")
123
+ print()
124
+
125
+ # 2. 测试数据库连接(失败情况)
126
+ print("2. 测试数据库连接(失败情况)")
127
+ try:
128
+ connect_to_db("invalid.host", 5432)
129
+ except Exception as e:
130
+ print(f"❌ 预期的数据库连接错误: {e}")
131
+ print()
132
+
133
+ # 3. 测试API调用(成功情况)
134
+ print("3. 测试API调用(成功情况)")
135
+ try:
136
+ data = await fetch_data_from_api("https://api.com/data")
137
+ process_data(data)
138
+ except Exception as e:
139
+ print(f"❌ API调用错误: {e}")
140
+ print()
141
+
142
+ # 4. 测试API调用(失败情况,带重试)
143
+ print("4. 测试API调用(失败情况,带重试)")
144
+ try:
145
+ data = await fetch_data_from_api("https://error.api.com/data")
146
+ process_data(data)
147
+ except Exception as e:
148
+ print(f"❌ API调用错误(重试后仍然失败): {e}")
149
+ print()
150
+
151
+ # 5. 测试数据处理(失败情况)
152
+ print("5. 测试数据处理(失败情况)")
153
+ process_data(None) # 空数据
154
+ process_data({"error": "invalid format"}) # 错误数据
155
+ print()
156
+
157
+ # 6. 查看错误历史
158
+ print("6. 错误历史记录")
159
+ history = error_handler.get_error_history()
160
+ print(f"共记录 {len(history)} 个错误:")
161
+ for i, record in enumerate(history, 1):
162
+ print(f" {i}. {record['exception_type']}: {record['message']}")
163
+ if record['context']:
164
+ print(f" 上下文: {record['context']}")
165
+ print()
166
+
167
+ print("=" * 50)
168
+ print("🎉 示例运行完成")
169
+
170
+
171
+ if __name__ == "__main__":
172
+ asyncio.run(main())
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Redis Key 验证演示脚本
5
+ 演示如何使用Redis Key验证工具
6
+ """
7
+ import sys
8
+ import os
9
+
10
+ # 添加项目根目录到路径
11
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
12
+
13
+ from crawlo.utils.redis_key_validator import (
14
+ RedisKeyValidator,
15
+ validate_redis_key_naming,
16
+ validate_multiple_redis_keys,
17
+ get_redis_key_info,
18
+ print_validation_report
19
+ )
20
+
21
+
22
+ def demonstrate_redis_key_validation():
23
+ """演示Redis Key验证功能"""
24
+ print("=== Redis Key 验证功能演示 ===\n")
25
+
26
+ # 1. 验证单个有效的Redis Key
27
+ print("1. 验证单个有效的Redis Key:")
28
+ valid_keys = [
29
+ "crawlo:books_distributed:filter:fingerprint",
30
+ "crawlo:books_distributed:queue:requests",
31
+ "crawlo:books_distributed:queue:processing",
32
+ "crawlo:books_distributed:queue:failed",
33
+ "crawlo:books_distributed:item:fingerprint"
34
+ ]
35
+
36
+ for key in valid_keys:
37
+ is_valid = validate_redis_key_naming(key, "books_distributed")
38
+ print(f" ✅ {key} - {'通过' if is_valid else '失败'}")
39
+
40
+ print()
41
+
42
+ # 2. 验证无效的Redis Key
43
+ print("2. 验证无效的Redis Key:")
44
+ invalid_keys = [
45
+ "invalid_format", # 不以crawlo开头
46
+ "crawlo:books_distributed", # 部分缺失
47
+ "crawlo:books_distributed:invalid_component:fingerprint", # 无效组件
48
+ "crawlo:books_distributed:queue:invalid_subcomponent", # 无效子组件
49
+ "crawlo:wrong_project:filter:fingerprint" # 项目名称不匹配
50
+ ]
51
+
52
+ for key in invalid_keys:
53
+ is_valid = validate_redis_key_naming(key, "books_distributed")
54
+ print(f" ❌ {key} - {'通过' if is_valid else '失败'}")
55
+
56
+ print()
57
+
58
+ # 3. 批量验证Redis Key
59
+ print("3. 批量验证Redis Key:")
60
+ all_keys = valid_keys + invalid_keys
61
+ is_valid, invalid_keys_list = validate_multiple_redis_keys(all_keys, "books_distributed")
62
+
63
+ print(f" 批量验证结果: {'全部通过' if is_valid else '存在无效Key'}")
64
+ if not is_valid:
65
+ print(" 无效的Key:")
66
+ for key in invalid_keys_list:
67
+ print(f" - {key}")
68
+
69
+ print()
70
+
71
+ # 4. 获取Redis Key信息
72
+ print("4. 获取Redis Key信息:")
73
+ sample_keys = [
74
+ "crawlo:api_data_collection:filter:fingerprint",
75
+ "crawlo:api_data_collection:queue:requests",
76
+ "crawlo:api_data_collection:item:fingerprint"
77
+ ]
78
+
79
+ for key in sample_keys:
80
+ info = get_redis_key_info(key)
81
+ print(f" Key: {key}")
82
+ if info['valid']:
83
+ print(f" 框架: {info['framework']}")
84
+ print(f" 项目: {info['project']}")
85
+ print(f" 组件: {info['component']}")
86
+ if 'sub_component' in info:
87
+ print(f" 子组件: {info['sub_component']}")
88
+ else:
89
+ print(f" 错误: {info.get('error', '无效')}")
90
+ print()
91
+
92
+ # 5. 打印验证报告
93
+ print("5. 打印验证报告:")
94
+ print_validation_report(all_keys, "books_distributed")
95
+
96
+
97
+ def demonstrate_advanced_validation():
98
+ """演示高级验证功能"""
99
+ print("\n=== 高级验证功能演示 ===\n")
100
+
101
+ validator = RedisKeyValidator()
102
+
103
+ # 1. 不指定项目名称的验证
104
+ print("1. 不指定项目名称的验证:")
105
+ key = "crawlo:any_project:filter:fingerprint"
106
+ is_valid = validator.validate_key_naming(key) # 不指定项目名称
107
+ print(f" {key} - {'通过' if is_valid else '失败'}")
108
+
109
+ # 2. 验证不同项目的Key
110
+ print("\n2. 验证不同项目的Key:")
111
+ project_keys = {
112
+ "books_distributed": [
113
+ "crawlo:books_distributed:filter:fingerprint",
114
+ "crawlo:books_distributed:queue:requests"
115
+ ],
116
+ "api_data_collection": [
117
+ "crawlo:api_data_collection:filter:fingerprint",
118
+ "crawlo:api_data_collection:queue:requests"
119
+ ]
120
+ }
121
+
122
+ for project_name, keys in project_keys.items():
123
+ print(f" 项目: {project_name}")
124
+ for key in keys:
125
+ is_valid = validator.validate_key_naming(key, project_name)
126
+ print(f" {key} - {'通过' if is_valid else '失败'}")
127
+
128
+
129
+ if __name__ == "__main__":
130
+ demonstrate_redis_key_validation()
131
+ demonstrate_advanced_validation()
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/python
2
+ # -*- coding:UTF-8 -*-
3
+ """
4
+ Response 改进功能使用示例
5
+ """
6
+ from crawlo.network.response import Response
7
+
8
+
9
+ def demo_response_improvements():
10
+ """演示Response改进功能"""
11
+ print("=== Response 改进功能演示 ===\n")
12
+
13
+ # 创建一个示例HTML响应
14
+ html_content = """
15
+ <html>
16
+ <head>
17
+ <title>Crawlo框架示例页面</title>
18
+ </head>
19
+ <body>
20
+ <div class="container">
21
+ <h1>产品列表</h1>
22
+ <div class="product-list">
23
+ <div class="product-item" data-id="1">
24
+ <h2>产品A</h2>
25
+ <p class="price">¥99.99</p>
26
+ <p class="description">这是产品A的描述信息</p>
27
+ <a href="/product/1" class="details-link">查看详情</a>
28
+ </div>
29
+ <div class="product-item" data-id="2">
30
+ <h2>产品B</h2>
31
+ <p class="price">¥149.99</p>
32
+ <p class="description">这是产品B的描述信息</p>
33
+ <a href="/product/2" class="details-link">查看详情</a>
34
+ </div>
35
+ <div class="product-item" data-id="3">
36
+ <h2>产品C</h2>
37
+ <p class="price">¥199.99</p>
38
+ <p class="description">这是产品C的描述信息</p>
39
+ <a href="/product/3" class="details-link">查看详情</a>
40
+ </div>
41
+ </div>
42
+ <div class="pagination">
43
+ <a href="/page/1" class="page-link">1</a>
44
+ <a href="/page/2" class="page-link active">2</a>
45
+ <a href="/page/3" class="page-link">3</a>
46
+ </div>
47
+ </div>
48
+ </body>
49
+ </html>
50
+ """
51
+
52
+ # 创建Response对象
53
+ response = Response(
54
+ url="https://example.com/products",
55
+ body=html_content.encode('utf-8'),
56
+ headers={"content-type": "text/html; charset=utf-8"}
57
+ )
58
+
59
+ # 1. 演示 extract_text 方法(支持CSS和XPath)
60
+ print("1. 提取文本内容:")
61
+ title = response.extract_text('title')
62
+ print(f" 页面标题: {title}")
63
+
64
+ # 使用CSS选择器提取第一个产品名称
65
+ first_product_name = response.extract_text('.product-item:first-child h2')
66
+ print(f" 第一个产品名称 (CSS): {first_product_name}")
67
+
68
+ # 使用XPath选择器
69
+ first_product_name_xpath = response.extract_text('//div[@class="product-item"][1]/h2')
70
+ print(f" 第一个产品名称 (XPath): {first_product_name_xpath}")
71
+
72
+ # 使用默认值处理不存在的元素
73
+ non_exist = response.extract_text('.non-exist', default='未找到')
74
+ print(f" 不存在的元素: {non_exist}")
75
+
76
+ print()
77
+
78
+ # 2. 演示 extract_texts 方法(提取多个元素的文本)
79
+ print("2. 提取多个元素的文本:")
80
+ # 提取所有产品名称
81
+ product_names = response.extract_texts('.product-item h2')
82
+ print(f" 所有产品名称: {product_names}")
83
+
84
+ # 提取所有价格
85
+ prices = response.extract_texts('.price')
86
+ print(f" 所有价格: {prices}")
87
+
88
+ # 使用XPath提取所有产品名称
89
+ product_names_xpath = response.extract_texts('//div[@class="product-item"]/h2')
90
+ print(f" 所有产品名称 (XPath): {product_names_xpath}")
91
+
92
+ print()
93
+
94
+ # 3. 演示 extract_attr 方法(提取元素属性)
95
+ print("3. 提取元素属性:")
96
+ # 提取第一个产品项的data-id属性
97
+ first_product_id = response.extract_attr('.product-item', 'data-id')
98
+ print(f" 第一个产品ID: {first_product_id}")
99
+
100
+ # 提取详情链接的href属性
101
+ first_detail_link = response.extract_attr('.details-link', 'href')
102
+ print(f" 第一个详情链接: {first_detail_link}")
103
+
104
+ # 提取不存在属性的默认值
105
+ non_exist_attr = response.extract_attr('.product-item', 'non-exist', default='默认值')
106
+ print(f" 不存在的属性: {non_exist_attr}")
107
+
108
+ print()
109
+
110
+ # 4. 演示 extract_attrs 方法(提取多个元素的属性)
111
+ print("4. 提取多个元素的属性:")
112
+ # 提取所有产品项的data-id属性
113
+ product_ids = response.extract_attrs('.product-item', 'data-id')
114
+ print(f" 所有产品ID: {product_ids}")
115
+
116
+ # 提取所有详情链接的href属性
117
+ detail_links = response.extract_attrs('.details-link', 'href')
118
+ print(f" 所有详情链接: {detail_links}")
119
+
120
+ # 提取分页链接的href属性
121
+ page_links = response.extract_attrs('.page-link', 'href')
122
+ print(f" 分页链接: {page_links}")
123
+
124
+ print()
125
+
126
+ # 5. 演示复杂文本提取
127
+ print("5. 复杂文本提取:")
128
+ # 提取所有产品描述
129
+ descriptions = response.extract_texts('.description')
130
+ print(f" 所有产品描述: {descriptions}")
131
+
132
+ print()
133
+
134
+ # 6. 边界情况处理
135
+ print("6. 边界情况处理:")
136
+ # 空响应测试
137
+ empty_response = Response(url="https://example.com/empty", body=b"")
138
+ empty_text = empty_response.extract_text('title', default='默认标题')
139
+ print(f" 空响应默认值: {empty_text}")
140
+
141
+ print("\n=== 演示完成 ===")
142
+
143
+
144
+ if __name__ == '__main__':
145
+ demo_response_improvements()