crawlo 1.1.2__py3-none-any.whl → 1.1.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 (113) hide show
  1. crawlo/__init__.py +34 -34
  2. crawlo/__version__.py +1 -1
  3. crawlo/cli.py +40 -40
  4. crawlo/commands/__init__.py +13 -13
  5. crawlo/commands/check.py +594 -594
  6. crawlo/commands/genspider.py +151 -151
  7. crawlo/commands/list.py +155 -155
  8. crawlo/commands/run.py +285 -285
  9. crawlo/commands/startproject.py +196 -196
  10. crawlo/commands/stats.py +188 -188
  11. crawlo/commands/utils.py +186 -186
  12. crawlo/config.py +279 -279
  13. crawlo/core/__init__.py +2 -2
  14. crawlo/core/engine.py +171 -171
  15. crawlo/core/enhanced_engine.py +189 -189
  16. crawlo/core/processor.py +40 -40
  17. crawlo/core/scheduler.py +166 -162
  18. crawlo/crawler.py +1027 -1027
  19. crawlo/downloader/__init__.py +242 -242
  20. crawlo/downloader/aiohttp_downloader.py +212 -212
  21. crawlo/downloader/cffi_downloader.py +251 -251
  22. crawlo/downloader/httpx_downloader.py +259 -257
  23. crawlo/event.py +11 -11
  24. crawlo/exceptions.py +82 -78
  25. crawlo/extension/__init__.py +31 -31
  26. crawlo/extension/log_interval.py +49 -49
  27. crawlo/extension/log_stats.py +44 -44
  28. crawlo/extension/logging_extension.py +34 -34
  29. crawlo/filters/__init__.py +154 -154
  30. crawlo/filters/aioredis_filter.py +242 -242
  31. crawlo/filters/memory_filter.py +269 -269
  32. crawlo/items/__init__.py +23 -23
  33. crawlo/items/base.py +21 -21
  34. crawlo/items/fields.py +53 -53
  35. crawlo/items/items.py +104 -104
  36. crawlo/middleware/__init__.py +21 -21
  37. crawlo/middleware/default_header.py +32 -32
  38. crawlo/middleware/download_delay.py +28 -28
  39. crawlo/middleware/middleware_manager.py +135 -135
  40. crawlo/middleware/proxy.py +248 -248
  41. crawlo/middleware/request_ignore.py +30 -30
  42. crawlo/middleware/response_code.py +18 -18
  43. crawlo/middleware/response_filter.py +26 -26
  44. crawlo/middleware/retry.py +125 -125
  45. crawlo/mode_manager.py +200 -200
  46. crawlo/network/__init__.py +21 -21
  47. crawlo/network/request.py +311 -311
  48. crawlo/network/response.py +271 -269
  49. crawlo/pipelines/__init__.py +22 -13
  50. crawlo/pipelines/bloom_dedup_pipeline.py +157 -0
  51. crawlo/pipelines/console_pipeline.py +39 -39
  52. crawlo/pipelines/csv_pipeline.py +316 -316
  53. crawlo/pipelines/database_dedup_pipeline.py +225 -0
  54. crawlo/pipelines/json_pipeline.py +218 -218
  55. crawlo/pipelines/memory_dedup_pipeline.py +116 -0
  56. crawlo/pipelines/mongo_pipeline.py +116 -116
  57. crawlo/pipelines/mysql_pipeline.py +195 -195
  58. crawlo/pipelines/pipeline_manager.py +56 -56
  59. crawlo/pipelines/redis_dedup_pipeline.py +163 -0
  60. crawlo/project.py +153 -153
  61. crawlo/queue/pqueue.py +37 -37
  62. crawlo/queue/queue_manager.py +307 -303
  63. crawlo/queue/redis_priority_queue.py +208 -191
  64. crawlo/settings/__init__.py +7 -7
  65. crawlo/settings/default_settings.py +245 -226
  66. crawlo/settings/setting_manager.py +99 -99
  67. crawlo/spider/__init__.py +639 -639
  68. crawlo/stats_collector.py +59 -59
  69. crawlo/subscriber.py +106 -106
  70. crawlo/task_manager.py +30 -30
  71. crawlo/templates/crawlo.cfg.tmpl +10 -10
  72. crawlo/templates/project/__init__.py.tmpl +3 -3
  73. crawlo/templates/project/items.py.tmpl +17 -17
  74. crawlo/templates/project/middlewares.py.tmpl +86 -86
  75. crawlo/templates/project/pipelines.py.tmpl +341 -335
  76. crawlo/templates/project/run.py.tmpl +251 -238
  77. crawlo/templates/project/settings.py.tmpl +250 -247
  78. crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
  79. crawlo/templates/spider/spider.py.tmpl +177 -177
  80. crawlo/utils/__init__.py +7 -7
  81. crawlo/utils/controlled_spider_mixin.py +439 -335
  82. crawlo/utils/date_tools.py +233 -233
  83. crawlo/utils/db_helper.py +343 -343
  84. crawlo/utils/func_tools.py +82 -82
  85. crawlo/utils/large_scale_config.py +286 -286
  86. crawlo/utils/large_scale_helper.py +343 -343
  87. crawlo/utils/log.py +128 -128
  88. crawlo/utils/queue_helper.py +175 -175
  89. crawlo/utils/request.py +267 -267
  90. crawlo/utils/request_serializer.py +219 -219
  91. crawlo/utils/spider_loader.py +62 -62
  92. crawlo/utils/system.py +11 -11
  93. crawlo/utils/tools.py +4 -4
  94. crawlo/utils/url.py +39 -39
  95. {crawlo-1.1.2.dist-info → crawlo-1.1.3.dist-info}/METADATA +635 -567
  96. crawlo-1.1.3.dist-info/RECORD +113 -0
  97. examples/__init__.py +7 -7
  98. examples/controlled_spider_example.py +205 -0
  99. tests/__init__.py +7 -7
  100. tests/test_final_validation.py +153 -153
  101. tests/test_proxy_health_check.py +32 -32
  102. tests/test_proxy_middleware_integration.py +136 -136
  103. tests/test_proxy_providers.py +56 -56
  104. tests/test_proxy_stats.py +19 -19
  105. tests/test_proxy_strategies.py +59 -59
  106. tests/test_redis_config.py +28 -28
  107. tests/test_redis_queue.py +224 -224
  108. tests/test_request_serialization.py +70 -70
  109. tests/test_scheduler.py +241 -241
  110. crawlo-1.1.2.dist-info/RECORD +0 -108
  111. {crawlo-1.1.2.dist-info → crawlo-1.1.3.dist-info}/WHEEL +0 -0
  112. {crawlo-1.1.2.dist-info → crawlo-1.1.3.dist-info}/entry_points.txt +0 -0
  113. {crawlo-1.1.2.dist-info → crawlo-1.1.3.dist-info}/top_level.txt +0 -0
@@ -1,248 +1,251 @@
1
- # -*- coding: UTF-8 -*-
2
- """
3
- {{project_name}} 项目配置文件
4
- =============================
5
- 基于 Crawlo 框架的爬虫项目配置。
6
-
7
- 🎯 快速开始:
8
-
9
- # 方式1:使用默认单机模式(推荐)
10
- from crawlo.crawler import CrawlerProcess
11
- process = CrawlerProcess() # 无需任何配置
12
-
13
- # 方式2:使用配置工厂
14
- from crawlo.config import CrawloConfig
15
- config = CrawloConfig.standalone() # 单机模式
16
- config = CrawloConfig.distributed(redis_host='192.168.1.100') # 分布式模式
17
- process = CrawlerProcess(settings=config.to_dict())
18
-
19
- # 方式3:使用环境变量
20
- from crawlo.config import CrawloConfig
21
- config = CrawloConfig.from_env() # 从环境变量读取
22
- """
23
- import os
24
- from crawlo.config import CrawloConfig
25
-
26
- # ============================== 项目基本信息 ==============================
27
- PROJECT_NAME = '{{project_name}}'
28
- VERSION = '1.0.0'
29
-
30
- # ============================== 运行模式选择 ==============================
31
-
32
- # 🎯 选择一种配置方式:
33
-
34
- # 方式1:使用配置工厂(推荐)
35
- # 单机模式(默认)
36
- CONFIG = CrawloConfig.standalone(
37
- concurrency=8,
38
- download_delay=1.0
39
- )
40
-
41
- # 分布式模式(去掉注释并修改 Redis 地址)
42
- # CONFIG = CrawloConfig.distributed(
43
- # redis_host='127.0.0.1',
44
- # redis_password='your_password', # 如果有密码
45
- # project_name='{{project_name}}',
46
- # concurrency=16,
47
- # download_delay=1.0
48
- # )
49
-
50
- # 自动检测模式
51
- # CONFIG = CrawloConfig.auto(concurrency=12)
52
-
53
- # 方式2:从环境变量读取(适合部署)
54
- # CONFIG = CrawloConfig.from_env()
55
-
56
- # 方式3:使用预设配置
57
- # from crawlo.config import Presets
58
- # CONFIG = Presets.development() # 开发环境
59
- # CONFIG = Presets.production() # 生产环境
60
-
61
- # 获取最终配置
62
- locals().update(CONFIG.to_dict())
63
-
64
- # ============================== 网络请求配置 ==============================
65
-
66
- # 下载器选择(推荐使用 CurlCffi,支持浏览器指纹模拟)
67
- DOWNLOADER = "crawlo.downloader.cffi_downloader.CurlCffiDownloader" # 支持浏览器指纹
68
- # DOWNLOADER = "crawlo.downloader.aiohttp_downloader.AioHttpDownloader" # 轻量级选择
69
- # DOWNLOADER = "crawlo.downloader.httpx_downloader.HttpXDownloader" # HTTP/2 支持
70
-
71
- # 请求超时与安全
72
- DOWNLOAD_TIMEOUT = 30
73
- VERIFY_SSL = True
74
- USE_SESSION = True
75
-
76
- # 请求延迟控制(防反爬)
77
- DOWNLOAD_DELAY = 1.0
78
- RANDOM_RANGE = (0.8, 1.2)
79
- RANDOMNESS = True
80
-
81
- # 重试策略
82
- MAX_RETRY_TIMES = 3
83
- RETRY_PRIORITY = -1
84
- RETRY_HTTP_CODES = [408, 429, 500, 502, 503, 504, 522, 524]
85
- IGNORE_HTTP_CODES = [403, 404]
86
- ALLOWED_CODES = []
87
-
88
- # 连接池配置
89
- CONNECTION_POOL_LIMIT = 50
90
- DOWNLOAD_MAXSIZE = 10 * 1024 * 1024 # 10MB
91
- DOWNLOAD_WARN_SIZE = 1024 * 1024 # 1MB
92
-
93
- # ============================== 并发与调度配置 ==============================
94
- CONCURRENCY = 8
95
- INTERVAL = 5
96
- DEPTH_PRIORITY = 1
97
- MAX_RUNNING_SPIDERS = 3
98
-
99
- # ============================== 队列配置(支持分布式) ==============================
100
-
101
- # 队列类型:'auto'(自动选择), 'memory'(内存队列), 'redis'(分布式队列)
102
- QUEUE_TYPE = 'auto'
103
- SCHEDULER_MAX_QUEUE_SIZE = 2000
104
- SCHEDULER_QUEUE_NAME = f'{{project_name}}:requests'
105
- QUEUE_MAX_RETRIES = 3
106
- QUEUE_TIMEOUT = 300
107
-
108
- # 大规模爬取优化
109
- LARGE_SCALE_BATCH_SIZE = 1000
110
- LARGE_SCALE_CHECKPOINT_INTERVAL = 5000
111
- LARGE_SCALE_MAX_MEMORY_USAGE = 500
112
-
113
- # ============================== 数据存储配置 ==============================
114
-
115
- # --- MySQL 配置 ---
116
- MYSQL_HOST = os.getenv('MYSQL_HOST', '127.0.0.1')
117
- MYSQL_PORT = int(os.getenv('MYSQL_PORT', 3306))
118
- MYSQL_USER = os.getenv('MYSQL_USER', 'root')
119
- MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', '123456')
120
- MYSQL_DB = os.getenv('MYSQL_DB', '{{project_name}}')
121
- MYSQL_TABLE = '{{project_name}}_data'
122
- MYSQL_BATCH_SIZE = 100
123
-
124
- # MySQL 连接池
125
- MYSQL_FLUSH_INTERVAL = 5
126
- MYSQL_POOL_MIN = 5
127
- MYSQL_POOL_MAX = 20
128
- MYSQL_ECHO = False
129
-
130
- # --- MongoDB 配置 ---
131
- MONGO_URI = os.getenv('MONGO_URI', 'mongodb://localhost:27017')
132
- MONGO_DATABASE = f'{{project_name}}_db'
133
- MONGO_COLLECTION = '{{project_name}}_items'
134
- MONGO_MAX_POOL_SIZE = 200
135
- MONGO_MIN_POOL_SIZE = 20
136
-
137
- # ============================== 去重过滤配置 ==============================
138
-
139
- REQUEST_DIR = '.'
140
-
141
- # 去重过滤器(推荐分布式项目使用 Redis 过滤器)
142
- FILTER_CLASS = 'crawlo.filters.memory_filter.MemoryFilter'
143
- # FILTER_CLASS = 'crawlo.filters.aioredis_filter.AioRedisFilter' # 分布式去重
144
-
145
- # --- Redis 配置(用于分布式去重和队列) ---
146
- REDIS_HOST = os.getenv('REDIS_HOST', '127.0.0.1')
147
- REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
148
- REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', '')
149
-
150
- # 根据是否有密码生成 URL
151
- if REDIS_PASSWORD:
152
- REDIS_URL = f'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/0'
153
- else:
154
- REDIS_URL = f'redis://{REDIS_HOST}:{REDIS_PORT}/0'
155
-
156
- REDIS_KEY = f'{{project_name}}:fingerprint'
157
- REDIS_TTL = 0
158
- CLEANUP_FP = 0
159
- FILTER_DEBUG = True
160
- DECODE_RESPONSES = True
161
-
162
- # ============================== 中间件配置 ==============================
163
-
164
- MIDDLEWARES = [
165
- # === 请求预处理阶段 ===
166
- 'crawlo.middleware.request_ignore.RequestIgnoreMiddleware',
167
- 'crawlo.middleware.download_delay.DownloadDelayMiddleware',
168
- 'crawlo.middleware.default_header.DefaultHeaderMiddleware',
169
- 'crawlo.middleware.proxy.ProxyMiddleware',
170
-
171
- # === 响应处理阶段 ===
172
- 'crawlo.middleware.retry.RetryMiddleware',
173
- 'crawlo.middleware.response_code.ResponseCodeMiddleware',
174
- 'crawlo.middleware.response_filter.ResponseFilterMiddleware',
175
- ]
176
-
177
- # ============================== 数据管道配置 ==============================
178
-
179
- PIPELINES = [
180
- 'crawlo.pipelines.console_pipeline.ConsolePipeline',
181
- # '{{project_name}}.pipelines.DatabasePipeline', # 自定义数据库管道
182
- # 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL 存储
183
- # 'crawlo.pipelines.mongo_pipeline.MongoPipeline', # MongoDB 存储
184
- ]
185
-
186
- # ============================== 扩展组件 ==============================
187
-
188
- EXTENSIONS = [
189
- 'crawlo.extension.log_interval.LogIntervalExtension',
190
- 'crawlo.extension.log_stats.LogStats',
191
- 'crawlo.extension.logging_extension.CustomLoggerExtension',
192
- ]
193
-
194
- # ============================== 日志配置 ==============================
195
-
196
- LOG_LEVEL = 'INFO'
197
- STATS_DUMP = True
198
- LOG_FILE = f'logs/{{project_name}}.log'
199
- LOG_FORMAT = '%(asctime)s - [%(name)s] - %(levelname)s: %(message)s'
200
- LOG_ENCODING = 'utf-8'
201
-
202
- # ============================== 代理配置 ==============================
203
-
204
- PROXY_ENABLED = False
205
- PROXY_API_URL = "" # 请填入真实的代理API地址
206
- PROXY_EXTRACTOR = "proxy"
207
- PROXY_REFRESH_INTERVAL = 60
208
- PROXY_API_TIMEOUT = 10
209
-
210
- # ============================== 浏览器指纹配置 ==============================
211
-
212
- # CurlCffi 下载器专用配置
213
- CURL_BROWSER_TYPE = "chrome"
214
- CURL_BROWSER_VERSION_MAP = {
215
- "chrome": "chrome136",
216
- "edge": "edge101",
217
- "safari": "safari184",
218
- "firefox": "firefox135",
219
- }
220
-
221
- # 默认请求头
222
- DEFAULT_REQUEST_HEADERS = {
223
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
224
- '(KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36',
225
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
226
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
227
- 'Accept-Encoding': 'gzip, deflate, br',
228
- 'Connection': 'keep-alive',
229
- 'Upgrade-Insecure-Requests': '1',
230
- }
231
-
232
- # ============================== 开发与调试 ==============================
233
-
234
- # 开发模式配置
235
- DEBUG = False
236
- TESTING = False
237
-
238
- # 性能监控
239
- ENABLE_PERFORMANCE_MONITORING = True
240
- MEMORY_USAGE_WARNING_THRESHOLD = 500 # MB
241
-
242
- # ============================== 自定义配置区域 ==============================
243
- # 在此处添加项目特定的配置项
244
-
245
- # 示例:目标网站特定配置
246
- # TARGET_DOMAIN = '{{domain}}'
247
- # MAX_PAGES_PER_DOMAIN = 10000
1
+ # -*- coding: UTF-8 -*-
2
+ """
3
+ {{project_name}} 项目配置文件
4
+ =============================
5
+ 基于 Crawlo 框架的爬虫项目配置。
6
+
7
+ 🎯 快速开始:
8
+
9
+ # 方式1:使用默认单机模式(推荐)
10
+ from crawlo.crawler import CrawlerProcess
11
+ process = CrawlerProcess() # 无需任何配置
12
+
13
+ # 方式2:使用配置工厂
14
+ from crawlo.config import CrawloConfig
15
+ config = CrawloConfig.standalone() # 单机模式
16
+ config = CrawloConfig.distributed(redis_host='192.168.1.100') # 分布式模式
17
+ process = CrawlerProcess(settings=config.to_dict())
18
+
19
+ # 方式3:使用环境变量
20
+ from crawlo.config import CrawloConfig
21
+ config = CrawloConfig.from_env() # 从环境变量读取
22
+ """
23
+ import os
24
+ from crawlo.config import CrawloConfig
25
+
26
+ # ============================== 项目基本信息 ==============================
27
+ PROJECT_NAME = '{{project_name}}'
28
+ VERSION = '1.0.0'
29
+
30
+ # ============================== 运行模式选择 ==============================
31
+
32
+ # 🎯 选择一种配置方式:
33
+
34
+ # 方式1:使用配置工厂(推荐)
35
+ # 单机模式(默认)
36
+ CONFIG = CrawloConfig.standalone(
37
+ concurrency=8,
38
+ download_delay=1.0
39
+ )
40
+
41
+ # 分布式模式(去掉注释并修改 Redis 地址)
42
+ # CONFIG = CrawloConfig.distributed(
43
+ # redis_host='127.0.0.1',
44
+ # redis_password='your_password', # 如果有密码
45
+ # project_name='{{project_name}}',
46
+ # concurrency=16,
47
+ # download_delay=1.0
48
+ # )
49
+
50
+ # 自动检测模式
51
+ # CONFIG = CrawloConfig.auto(concurrency=12)
52
+
53
+ # 方式2:从环境变量读取(适合部署)
54
+ # CONFIG = CrawloConfig.from_env()
55
+
56
+ # 方式3:使用预设配置
57
+ # from crawlo.config import Presets
58
+ # CONFIG = Presets.development() # 开发环境
59
+ # CONFIG = Presets.production() # 生产环境
60
+
61
+ # 获取最终配置
62
+ locals().update(CONFIG.to_dict())
63
+
64
+ # ============================== 网络请求配置 ==============================
65
+
66
+ # 下载器选择(推荐使用 CurlCffi,支持浏览器指纹模拟)
67
+ DOWNLOADER = "crawlo.downloader.cffi_downloader.CurlCffiDownloader" # 支持浏览器指纹
68
+ # DOWNLOADER = "crawlo.downloader.aiohttp_downloader.AioHttpDownloader" # 轻量级选择
69
+ # DOWNLOADER = "crawlo.downloader.httpx_downloader.HttpXDownloader" # HTTP/2 支持
70
+
71
+ # 请求超时与安全
72
+ DOWNLOAD_TIMEOUT = 30
73
+ VERIFY_SSL = True
74
+ USE_SESSION = True
75
+
76
+ # 请求延迟控制(防反爬)
77
+ DOWNLOAD_DELAY = 1.0
78
+ RANDOM_RANGE = (0.8, 1.2)
79
+ RANDOMNESS = True
80
+
81
+ # 重试策略
82
+ MAX_RETRY_TIMES = 3
83
+ RETRY_PRIORITY = -1
84
+ RETRY_HTTP_CODES = [408, 429, 500, 502, 503, 504, 522, 524]
85
+ IGNORE_HTTP_CODES = [403, 404]
86
+ ALLOWED_CODES = []
87
+
88
+ # 连接池配置
89
+ CONNECTION_POOL_LIMIT = 50
90
+ DOWNLOAD_MAXSIZE = 10 * 1024 * 1024 # 10MB
91
+ DOWNLOAD_WARN_SIZE = 1024 * 1024 # 1MB
92
+
93
+ # ============================== 并发与调度配置 ==============================
94
+ CONCURRENCY = 8
95
+ INTERVAL = 5
96
+ DEPTH_PRIORITY = 1
97
+ MAX_RUNNING_SPIDERS = 3
98
+
99
+ # ============================== 队列配置(支持分布式) ==============================
100
+
101
+ # 队列类型:'auto'(自动选择), 'memory'(内存队列), 'redis'(分布式队列)
102
+ QUEUE_TYPE = 'auto'
103
+ SCHEDULER_MAX_QUEUE_SIZE = 2000
104
+ SCHEDULER_QUEUE_NAME = f'{{project_name}}:requests'
105
+ QUEUE_MAX_RETRIES = 3
106
+ QUEUE_TIMEOUT = 300
107
+
108
+ # 大规模爬取优化
109
+ LARGE_SCALE_BATCH_SIZE = 1000
110
+ LARGE_SCALE_CHECKPOINT_INTERVAL = 5000
111
+ LARGE_SCALE_MAX_MEMORY_USAGE = 500
112
+
113
+ # ============================== 数据存储配置 ==============================
114
+
115
+ # --- MySQL 配置 ---
116
+ MYSQL_HOST = os.getenv('MYSQL_HOST', '127.0.0.1')
117
+ MYSQL_PORT = int(os.getenv('MYSQL_PORT', 3306))
118
+ MYSQL_USER = os.getenv('MYSQL_USER', 'root')
119
+ MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', '123456')
120
+ MYSQL_DB = os.getenv('MYSQL_DB', '{{project_name}}')
121
+ MYSQL_TABLE = '{{project_name}}_data'
122
+ MYSQL_BATCH_SIZE = 100
123
+
124
+ # MySQL 连接池
125
+ MYSQL_FLUSH_INTERVAL = 5
126
+ MYSQL_POOL_MIN = 5
127
+ MYSQL_POOL_MAX = 20
128
+ MYSQL_ECHO = False
129
+
130
+ # --- MongoDB 配置 ---
131
+ MONGO_URI = os.getenv('MONGO_URI', 'mongodb://localhost:27017')
132
+ MONGO_DATABASE = f'{{project_name}}_db'
133
+ MONGO_COLLECTION = '{{project_name}}_items'
134
+ MONGO_MAX_POOL_SIZE = 200
135
+ MONGO_MIN_POOL_SIZE = 20
136
+
137
+ # ============================== 去重过滤配置 ==============================
138
+
139
+ REQUEST_DIR = '.'
140
+
141
+ # 去重过滤器(推荐分布式项目使用 Redis 过滤器)
142
+ FILTER_CLASS = 'crawlo.filters.memory_filter.MemoryFilter'
143
+ # FILTER_CLASS = 'crawlo.filters.aioredis_filter.AioRedisFilter' # 分布式去重
144
+
145
+ # --- Redis 配置(用于分布式去重和队列) ---
146
+ REDIS_HOST = os.getenv('REDIS_HOST', '127.0.0.1')
147
+ REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
148
+ REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', '')
149
+
150
+ # 根据是否有密码生成 URL
151
+ if REDIS_PASSWORD:
152
+ REDIS_URL = f'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/0'
153
+ else:
154
+ REDIS_URL = f'redis://{REDIS_HOST}:{REDIS_PORT}/0'
155
+
156
+ REDIS_KEY = f'{{project_name}}:fingerprint'
157
+ REDIS_TTL = 0
158
+ CLEANUP_FP = 0
159
+ FILTER_DEBUG = True
160
+ DECODE_RESPONSES = True
161
+
162
+ # ============================== 中间件配置 ==============================
163
+
164
+ MIDDLEWARES = [
165
+ # === 请求预处理阶段 ===
166
+ 'crawlo.middleware.request_ignore.RequestIgnoreMiddleware',
167
+ 'crawlo.middleware.download_delay.DownloadDelayMiddleware',
168
+ 'crawlo.middleware.default_header.DefaultHeaderMiddleware',
169
+ 'crawlo.middleware.proxy.ProxyMiddleware',
170
+
171
+ # === 响应处理阶段 ===
172
+ 'crawlo.middleware.retry.RetryMiddleware',
173
+ 'crawlo.middleware.response_code.ResponseCodeMiddleware',
174
+ 'crawlo.middleware.response_filter.ResponseFilterMiddleware',
175
+ ]
176
+
177
+ # ============================== 数据管道配置 ==============================
178
+
179
+ PIPELINES = [
180
+ # 根据运行模式自动选择默认去重管道
181
+ # 单机模式:crawlo.pipelines.MemoryDedupPipeline
182
+ # 分布式模式:crawlo.pipelines.RedisDedupPipeline
183
+ 'crawlo.pipelines.console_pipeline.ConsolePipeline',
184
+ # '{{project_name}}.pipelines.DatabasePipeline', # 自定义数据库管道
185
+ # 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL 存储
186
+ # 'crawlo.pipelines.mongo_pipeline.MongoPipeline', # MongoDB 存储
187
+ ]
188
+
189
+ # ============================== 扩展组件 ==============================
190
+
191
+ EXTENSIONS = [
192
+ 'crawlo.extension.log_interval.LogIntervalExtension',
193
+ 'crawlo.extension.log_stats.LogStats',
194
+ 'crawlo.extension.logging_extension.CustomLoggerExtension',
195
+ ]
196
+
197
+ # ============================== 日志配置 ==============================
198
+
199
+ LOG_LEVEL = 'INFO'
200
+ STATS_DUMP = True
201
+ LOG_FILE = f'logs/{{project_name}}.log'
202
+ LOG_FORMAT = '%(asctime)s - [%(name)s] - %(levelname)s: %(message)s'
203
+ LOG_ENCODING = 'utf-8'
204
+
205
+ # ============================== 代理配置 ==============================
206
+
207
+ PROXY_ENABLED = False
208
+ PROXY_API_URL = "" # 请填入真实的代理API地址
209
+ PROXY_EXTRACTOR = "proxy"
210
+ PROXY_REFRESH_INTERVAL = 60
211
+ PROXY_API_TIMEOUT = 10
212
+
213
+ # ============================== 浏览器指纹配置 ==============================
214
+
215
+ # CurlCffi 下载器专用配置
216
+ CURL_BROWSER_TYPE = "chrome"
217
+ CURL_BROWSER_VERSION_MAP = {
218
+ "chrome": "chrome136",
219
+ "edge": "edge101",
220
+ "safari": "safari184",
221
+ "firefox": "firefox135",
222
+ }
223
+
224
+ # 默认请求头
225
+ DEFAULT_REQUEST_HEADERS = {
226
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
227
+ '(KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36',
228
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
229
+ 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
230
+ 'Accept-Encoding': 'gzip, deflate, br',
231
+ 'Connection': 'keep-alive',
232
+ 'Upgrade-Insecure-Requests': '1',
233
+ }
234
+
235
+ # ============================== 开发与调试 ==============================
236
+
237
+ # 开发模式配置
238
+ DEBUG = False
239
+ TESTING = False
240
+
241
+ # 性能监控
242
+ ENABLE_PERFORMANCE_MONITORING = True
243
+ MEMORY_USAGE_WARNING_THRESHOLD = 500 # MB
244
+
245
+ # ============================== 自定义配置区域 ==============================
246
+ # 在此处添加项目特定的配置项
247
+
248
+ # 示例:目标网站特定配置
249
+ # TARGET_DOMAIN = '{{domain}}'
250
+ # MAX_PAGES_PER_DOMAIN = 10000
248
251
  # CUSTOM_RATE_LIMIT = 1.5
@@ -1,6 +1,6 @@
1
- # -*- coding: UTF-8 -*-
2
- """
3
- {{project_name}}.spiders
4
- ========================
5
- 存放所有的爬虫。
1
+ # -*- coding: UTF-8 -*-
2
+ """
3
+ {{project_name}}.spiders
4
+ ========================
5
+ 存放所有的爬虫。
6
6
  """