crawlo 1.1.8__py3-none-any.whl → 1.1.9__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 (190) hide show
  1. crawlo/__init__.py +61 -61
  2. crawlo/__version__.py +1 -1
  3. crawlo/cleaners/__init__.py +60 -60
  4. crawlo/cleaners/data_formatter.py +225 -225
  5. crawlo/cleaners/encoding_converter.py +125 -125
  6. crawlo/cleaners/text_cleaner.py +232 -232
  7. crawlo/cli.py +65 -65
  8. crawlo/commands/__init__.py +14 -14
  9. crawlo/commands/check.py +594 -594
  10. crawlo/commands/genspider.py +151 -151
  11. crawlo/commands/help.py +132 -132
  12. crawlo/commands/list.py +155 -155
  13. crawlo/commands/run.py +292 -292
  14. crawlo/commands/startproject.py +418 -418
  15. crawlo/commands/stats.py +188 -188
  16. crawlo/commands/utils.py +186 -186
  17. crawlo/config.py +312 -312
  18. crawlo/config_validator.py +252 -252
  19. crawlo/core/__init__.py +2 -2
  20. crawlo/core/engine.py +345 -345
  21. crawlo/core/processor.py +40 -40
  22. crawlo/core/scheduler.py +136 -136
  23. crawlo/crawler.py +1027 -1027
  24. crawlo/downloader/__init__.py +266 -266
  25. crawlo/downloader/aiohttp_downloader.py +220 -220
  26. crawlo/downloader/cffi_downloader.py +256 -256
  27. crawlo/downloader/httpx_downloader.py +259 -259
  28. crawlo/downloader/hybrid_downloader.py +213 -213
  29. crawlo/downloader/playwright_downloader.py +402 -402
  30. crawlo/downloader/selenium_downloader.py +472 -472
  31. crawlo/event.py +11 -11
  32. crawlo/exceptions.py +81 -81
  33. crawlo/extension/__init__.py +37 -37
  34. crawlo/extension/health_check.py +141 -141
  35. crawlo/extension/log_interval.py +57 -57
  36. crawlo/extension/log_stats.py +81 -81
  37. crawlo/extension/logging_extension.py +43 -43
  38. crawlo/extension/memory_monitor.py +104 -104
  39. crawlo/extension/performance_profiler.py +133 -133
  40. crawlo/extension/request_recorder.py +107 -107
  41. crawlo/filters/__init__.py +154 -154
  42. crawlo/filters/aioredis_filter.py +280 -280
  43. crawlo/filters/memory_filter.py +269 -269
  44. crawlo/items/__init__.py +23 -23
  45. crawlo/items/base.py +21 -21
  46. crawlo/items/fields.py +53 -53
  47. crawlo/items/items.py +104 -104
  48. crawlo/middleware/__init__.py +21 -21
  49. crawlo/middleware/default_header.py +32 -32
  50. crawlo/middleware/download_delay.py +28 -28
  51. crawlo/middleware/middleware_manager.py +135 -135
  52. crawlo/middleware/proxy.py +272 -272
  53. crawlo/middleware/request_ignore.py +30 -30
  54. crawlo/middleware/response_code.py +18 -18
  55. crawlo/middleware/response_filter.py +26 -26
  56. crawlo/middleware/retry.py +124 -124
  57. crawlo/mode_manager.py +211 -211
  58. crawlo/network/__init__.py +21 -21
  59. crawlo/network/request.py +338 -338
  60. crawlo/network/response.py +359 -359
  61. crawlo/pipelines/__init__.py +21 -21
  62. crawlo/pipelines/bloom_dedup_pipeline.py +156 -156
  63. crawlo/pipelines/console_pipeline.py +39 -39
  64. crawlo/pipelines/csv_pipeline.py +316 -316
  65. crawlo/pipelines/database_dedup_pipeline.py +224 -224
  66. crawlo/pipelines/json_pipeline.py +218 -218
  67. crawlo/pipelines/memory_dedup_pipeline.py +115 -115
  68. crawlo/pipelines/mongo_pipeline.py +131 -131
  69. crawlo/pipelines/mysql_pipeline.py +316 -316
  70. crawlo/pipelines/pipeline_manager.py +61 -61
  71. crawlo/pipelines/redis_dedup_pipeline.py +167 -167
  72. crawlo/project.py +187 -187
  73. crawlo/queue/pqueue.py +37 -37
  74. crawlo/queue/queue_manager.py +334 -334
  75. crawlo/queue/redis_priority_queue.py +298 -298
  76. crawlo/settings/__init__.py +7 -7
  77. crawlo/settings/default_settings.py +219 -219
  78. crawlo/settings/setting_manager.py +122 -122
  79. crawlo/spider/__init__.py +639 -639
  80. crawlo/stats_collector.py +59 -59
  81. crawlo/subscriber.py +130 -130
  82. crawlo/task_manager.py +30 -30
  83. crawlo/templates/crawlo.cfg.tmpl +10 -10
  84. crawlo/templates/project/__init__.py.tmpl +3 -3
  85. crawlo/templates/project/items.py.tmpl +17 -17
  86. crawlo/templates/project/middlewares.py.tmpl +109 -109
  87. crawlo/templates/project/pipelines.py.tmpl +96 -96
  88. crawlo/templates/project/run.py.tmpl +45 -45
  89. crawlo/templates/project/settings.py.tmpl +326 -326
  90. crawlo/templates/project/settings_distributed.py.tmpl +119 -119
  91. crawlo/templates/project/settings_gentle.py.tmpl +94 -94
  92. crawlo/templates/project/settings_high_performance.py.tmpl +151 -151
  93. crawlo/templates/project/settings_simple.py.tmpl +68 -68
  94. crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
  95. crawlo/templates/spider/spider.py.tmpl +141 -141
  96. crawlo/tools/__init__.py +182 -182
  97. crawlo/tools/anti_crawler.py +268 -268
  98. crawlo/tools/authenticated_proxy.py +240 -240
  99. crawlo/tools/data_validator.py +180 -180
  100. crawlo/tools/date_tools.py +35 -35
  101. crawlo/tools/distributed_coordinator.py +386 -386
  102. crawlo/tools/retry_mechanism.py +220 -220
  103. crawlo/tools/scenario_adapter.py +262 -262
  104. crawlo/utils/__init__.py +35 -35
  105. crawlo/utils/batch_processor.py +260 -260
  106. crawlo/utils/controlled_spider_mixin.py +439 -439
  107. crawlo/utils/date_tools.py +290 -290
  108. crawlo/utils/db_helper.py +343 -343
  109. crawlo/utils/enhanced_error_handler.py +359 -359
  110. crawlo/utils/env_config.py +105 -105
  111. crawlo/utils/error_handler.py +125 -125
  112. crawlo/utils/func_tools.py +82 -82
  113. crawlo/utils/large_scale_config.py +286 -286
  114. crawlo/utils/large_scale_helper.py +343 -343
  115. crawlo/utils/log.py +128 -128
  116. crawlo/utils/performance_monitor.py +284 -284
  117. crawlo/utils/queue_helper.py +175 -175
  118. crawlo/utils/redis_connection_pool.py +334 -334
  119. crawlo/utils/redis_key_validator.py +199 -199
  120. crawlo/utils/request.py +267 -267
  121. crawlo/utils/request_serializer.py +219 -219
  122. crawlo/utils/spider_loader.py +62 -62
  123. crawlo/utils/system.py +11 -11
  124. crawlo/utils/tools.py +4 -4
  125. crawlo/utils/url.py +39 -39
  126. {crawlo-1.1.8.dist-info → crawlo-1.1.9.dist-info}/METADATA +626 -626
  127. crawlo-1.1.9.dist-info/RECORD +190 -0
  128. examples/__init__.py +7 -7
  129. tests/DOUBLE_CRAWLO_PREFIX_FIX_REPORT.md +81 -81
  130. tests/__init__.py +7 -7
  131. tests/advanced_tools_example.py +275 -275
  132. tests/authenticated_proxy_example.py +236 -236
  133. tests/cleaners_example.py +160 -160
  134. tests/config_validation_demo.py +102 -102
  135. tests/controlled_spider_example.py +205 -205
  136. tests/date_tools_example.py +180 -180
  137. tests/dynamic_loading_example.py +523 -523
  138. tests/dynamic_loading_test.py +104 -104
  139. tests/env_config_example.py +133 -133
  140. tests/error_handling_example.py +171 -171
  141. tests/redis_key_validation_demo.py +130 -130
  142. tests/response_improvements_example.py +144 -144
  143. tests/test_advanced_tools.py +148 -148
  144. tests/test_all_redis_key_configs.py +145 -145
  145. tests/test_authenticated_proxy.py +141 -141
  146. tests/test_cleaners.py +54 -54
  147. tests/test_comprehensive.py +146 -146
  148. tests/test_config_validator.py +193 -193
  149. tests/test_date_tools.py +123 -123
  150. tests/test_double_crawlo_fix.py +207 -207
  151. tests/test_double_crawlo_fix_simple.py +124 -124
  152. tests/test_dynamic_downloaders_proxy.py +124 -124
  153. tests/test_dynamic_proxy.py +92 -92
  154. tests/test_dynamic_proxy_config.py +146 -146
  155. tests/test_dynamic_proxy_real.py +109 -109
  156. tests/test_edge_cases.py +303 -303
  157. tests/test_enhanced_error_handler.py +270 -270
  158. tests/test_env_config.py +121 -121
  159. tests/test_error_handler_compatibility.py +112 -112
  160. tests/test_final_validation.py +153 -153
  161. tests/test_framework_env_usage.py +103 -103
  162. tests/test_integration.py +356 -356
  163. tests/test_item_dedup_redis_key.py +122 -122
  164. tests/test_parsel.py +29 -29
  165. tests/test_performance.py +327 -327
  166. tests/test_proxy_health_check.py +32 -32
  167. tests/test_proxy_middleware_integration.py +136 -136
  168. tests/test_proxy_providers.py +56 -56
  169. tests/test_proxy_stats.py +19 -19
  170. tests/test_proxy_strategies.py +59 -59
  171. tests/test_queue_manager_double_crawlo.py +230 -230
  172. tests/test_queue_manager_redis_key.py +176 -176
  173. tests/test_redis_config.py +28 -28
  174. tests/test_redis_connection_pool.py +294 -294
  175. tests/test_redis_key_naming.py +181 -181
  176. tests/test_redis_key_validator.py +123 -123
  177. tests/test_redis_queue.py +224 -224
  178. tests/test_request_serialization.py +70 -70
  179. tests/test_response_improvements.py +152 -152
  180. tests/test_scheduler.py +241 -241
  181. tests/test_simple_response.py +61 -61
  182. tests/test_telecom_spider_redis_key.py +205 -205
  183. tests/test_template_content.py +87 -87
  184. tests/test_template_redis_key.py +134 -134
  185. tests/test_tools.py +153 -153
  186. tests/tools_example.py +257 -257
  187. crawlo-1.1.8.dist-info/RECORD +0 -190
  188. {crawlo-1.1.8.dist-info → crawlo-1.1.9.dist-info}/WHEEL +0 -0
  189. {crawlo-1.1.8.dist-info → crawlo-1.1.9.dist-info}/entry_points.txt +0 -0
  190. {crawlo-1.1.8.dist-info → crawlo-1.1.9.dist-info}/top_level.txt +0 -0
@@ -1,360 +1,360 @@
1
- #!/usr/bin/python
2
- # -*- coding:UTF-8 -*-
3
- """
4
- HTTP Response 封装模块
5
- =====================
6
- 提供功能丰富的HTTP响应封装,支持:
7
- - 智能编码检测和解码
8
- - XPath/CSS 选择器
9
- - JSON 解析和缓存
10
- - 正则表达式支持
11
- - Cookie 处理
12
- """
13
- import re
14
- import ujson
15
- from http.cookies import SimpleCookie
16
- from parsel import Selector, SelectorList
17
- from typing import Dict, Any, List, Optional, Union
18
- from urllib.parse import urljoin as _urljoin
19
-
20
- from crawlo.exceptions import DecodeError
21
-
22
-
23
- class Response:
24
- """
25
- HTTP响应的封装,提供数据解析的便捷方法。
26
-
27
- 功能特性:
28
- - 智能编码检测和缓存
29
- - 懒加载 Selector 实例
30
- - JSON 解析和缓存
31
- - 多类型数据提取
32
- """
33
-
34
- def __init__(
35
- self,
36
- url: str,
37
- *,
38
- headers: Dict[str, Any] = None,
39
- body: bytes = b"",
40
- method: str = 'GET',
41
- request: 'Request' = None, # 使用字符串注解避免循环导入
42
- status_code: int = 200,
43
- ):
44
- # 基本属性
45
- self.url = url
46
- self.headers = headers or {}
47
- self.body = body
48
- self.method = method.upper()
49
- self.request = request
50
- self.status_code = status_code
51
-
52
- # 编码处理
53
- self.encoding = self._determine_encoding()
54
-
55
- # 缓存属性
56
- self._text_cache = None
57
- self._json_cache = None
58
- self._selector_instance = None
59
-
60
- # 状态标记
61
- self._is_success = 200 <= status_code < 300
62
- self._is_redirect = 300 <= status_code < 400
63
- self._is_client_error = 400 <= status_code < 500
64
- self._is_server_error = status_code >= 500
65
-
66
- def _determine_encoding(self) -> Optional[str]:
67
- """智能检测响应编码"""
68
- # 1. 优先使用 request 的编码
69
- if self.request and self.request.encoding:
70
- return self.request.encoding
71
-
72
- # 2. 从 Content-Type 头中检测
73
- content_type = self.headers.get("content-type", "") or self.headers.get("Content-Type", "")
74
- if content_type:
75
- charset_match = re.search(r"charset=([w-]+)", content_type, re.I)
76
- if charset_match:
77
- return charset_match.group(1).lower()
78
-
79
- # 3. 从 HTML meta 标签中检测(仅对HTML内容)
80
- if b'<html' in self.body[:1024].lower():
81
- # 查找 <meta charset="xxx"> 或 <meta http-equiv="Content-Type" content="...charset=xxx">
82
- html_start = self.body[:4096] # 只检查前4KB
83
- try:
84
- html_text = html_start.decode('ascii', errors='ignore')
85
- # <meta charset="utf-8">
86
- charset_match = re.search(r'<meta[^>]+charset=["\']?([\w-]+)', html_text, re.I)
87
- if charset_match:
88
- return charset_match.group(1).lower()
89
-
90
- # <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
91
- content_match = re.search(r'<meta[^>]+content=["\'][^"\'>]*charset=([\w-]+)', html_text, re.I)
92
- if content_match:
93
- return content_match.group(1).lower()
94
- except Exception:
95
- pass
96
-
97
- # 4. 默认使用 utf-8
98
- return 'utf-8'
99
-
100
- @property
101
- def text(self) -> str:
102
- """将响应体(body)以正确的编码解码为字符串,并缓存结果。"""
103
- if self._text_cache is not None:
104
- return self._text_cache
105
-
106
- if not self.body:
107
- self._text_cache = ""
108
- return self._text_cache
109
-
110
- # 尝试多种编码
111
- encodings_to_try = [self.encoding]
112
- if self.encoding != 'utf-8':
113
- encodings_to_try.append('utf-8')
114
- if 'gbk' not in encodings_to_try:
115
- encodings_to_try.append('gbk')
116
- if 'gb2312' not in encodings_to_try:
117
- encodings_to_try.append('gb2312')
118
- encodings_to_try.append('latin1') # 最后的回退选项
119
-
120
- for encoding in encodings_to_try:
121
- if not encoding:
122
- continue
123
- try:
124
- self._text_cache = self.body.decode(encoding)
125
- return self._text_cache
126
- except (UnicodeDecodeError, LookupError):
127
- continue
128
-
129
- # 所有编码都失败,使用容错解码
130
- try:
131
- self._text_cache = self.body.decode('utf-8', errors='replace')
132
- return self._text_cache
133
- except Exception as e:
134
- raise DecodeError(f"Failed to decode response from {self.url}: {e}")
135
-
136
- @property
137
- def is_success(self) -> bool:
138
- """检查响应是否成功 (2xx)"""
139
- return self._is_success
140
-
141
- @property
142
- def is_redirect(self) -> bool:
143
- """检查响应是否为重定向 (3xx)"""
144
- return self._is_redirect
145
-
146
- @property
147
- def is_client_error(self) -> bool:
148
- """检查响应是否为客户端错误 (4xx)"""
149
- return self._is_client_error
150
-
151
- @property
152
- def is_server_error(self) -> bool:
153
- """检查响应是否为服务器错误 (5xx)"""
154
- return self._is_server_error
155
-
156
- @property
157
- def content_type(self) -> str:
158
- """获取响应的 Content-Type"""
159
- return self.headers.get('content-type', '') or self.headers.get('Content-Type', '')
160
-
161
- @property
162
- def content_length(self) -> Optional[int]:
163
- """获取响应的 Content-Length"""
164
- length = self.headers.get('content-length') or self.headers.get('Content-Length')
165
- return int(length) if length else None
166
-
167
- def json(self, default: Any = None) -> Any:
168
- """将响应文本解析为 JSON 对象。"""
169
- if self._json_cache is not None:
170
- return self._json_cache
171
-
172
- try:
173
- self._json_cache = ujson.loads(self.text)
174
- return self._json_cache
175
- except (ujson.JSONDecodeError, ValueError) as e:
176
- if default is not None:
177
- return default
178
- raise DecodeError(f"Failed to parse JSON from {self.url}: {e}")
179
-
180
- def urljoin(self, url: str) -> str:
181
- """拼接 URL,自动处理相对路径。"""
182
- return _urljoin(self.url, url)
183
-
184
- @property
185
- def _selector(self) -> Selector:
186
- """懒加载 Selector 实例"""
187
- if self._selector_instance is None:
188
- self._selector_instance = Selector(self.text)
189
- return self._selector_instance
190
-
191
- def xpath(self, query: str) -> SelectorList:
192
- """使用 XPath 选择器查询文档。"""
193
- return self._selector.xpath(query)
194
-
195
- def css(self, query: str) -> SelectorList:
196
- """使用 CSS 选择器查询文档。"""
197
- return self._selector.css(query)
198
-
199
- def _is_xpath(self, query: str) -> bool:
200
- """判断查询语句是否为XPath"""
201
- return query.startswith(('/', '//', './'))
202
-
203
- def _extract_text_from_elements(self, elements: SelectorList, join_str: str = " ") -> str:
204
- """
205
- 从元素列表中提取文本并拼接
206
-
207
- :param elements: SelectorList元素列表
208
- :param join_str: 文本拼接分隔符
209
- :return: 拼接后的文本
210
- """
211
- texts = []
212
- for element in elements:
213
- # 获取元素的所有文本节点
214
- if hasattr(element, 'xpath'):
215
- element_texts = element.xpath('.//text()').getall()
216
- else:
217
- element_texts = [str(element)]
218
- # 清理并添加非空文本
219
- for text in element_texts:
220
- cleaned = text.strip()
221
- if cleaned:
222
- texts.append(cleaned)
223
- return join_str.join(texts)
224
-
225
- def extract_text(self, xpath_or_css: str, join_str: str = " ", default: str = '') -> str:
226
- """
227
- 提取单个元素的文本内容,支持CSS和XPath选择器
228
-
229
- 参数:
230
- xpath_or_css: XPath或CSS选择器
231
- join_str: 文本拼接分隔符(默认为空格)
232
- default: 默认返回值,当未找到元素时返回
233
-
234
- 返回:
235
- 拼接后的纯文本字符串
236
- """
237
- try:
238
- elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
239
- if not elements:
240
- return default
241
- return self._extract_text_from_elements(elements, join_str)
242
- except Exception:
243
- return default
244
-
245
- def extract_texts(self, xpath_or_css: str, join_str: str = " ", default: List[str] = None) -> List[str]:
246
- """
247
- 提取多个元素的文本内容列表,支持CSS和XPath选择器
248
-
249
- 参数:
250
- xpath_or_css: XPath或CSS选择器
251
- join_str: 单个节点内文本拼接分隔符
252
- default: 默认返回值,当未找到元素时返回
253
-
254
- 返回:
255
- 纯文本列表(每个元素对应一个节点的文本)
256
- """
257
- if default is None:
258
- default = []
259
-
260
- try:
261
- elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
262
- if not elements:
263
- return default
264
-
265
- result = []
266
- for element in elements:
267
- # 对每个元素提取文本
268
- if hasattr(element, 'xpath'):
269
- texts = element.xpath('.//text()').getall()
270
- else:
271
- texts = [str(element)]
272
-
273
- # 清理文本并拼接
274
- clean_texts = [text.strip() for text in texts if text.strip()]
275
- if clean_texts:
276
- result.append(join_str.join(clean_texts))
277
-
278
- return result if result else default
279
- except Exception:
280
- return default
281
-
282
- def extract_attr(self, xpath_or_css: str, attr_name: str, default: Any = None) -> Any:
283
- """
284
- 提取单个元素的属性值,支持CSS和XPath选择器
285
-
286
- 参数:
287
- xpath_or_css: XPath或CSS选择器
288
- attr_name: 属性名称
289
- default: 默认返回值
290
-
291
- 返回:
292
- 属性值或默认值
293
- """
294
- try:
295
- elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
296
- if not elements:
297
- return default
298
- return elements.attrib.get(attr_name, default)
299
- except Exception:
300
- return default
301
-
302
- def extract_attrs(self, xpath_or_css: str, attr_name: str, default: List[Any] = None) -> List[Any]:
303
- """
304
- 提取多个元素的属性值列表,支持CSS和XPath选择器
305
-
306
- 参数:
307
- xpath_or_css: XPath或CSS选择器
308
- attr_name: 属性名称
309
- default: 默认返回值
310
-
311
- 返回:
312
- 属性值列表
313
- """
314
- if default is None:
315
- default = []
316
-
317
- try:
318
- elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
319
- if not elements:
320
- return default
321
-
322
- result = []
323
- for element in elements:
324
- if hasattr(element, 'attrib'):
325
- attr_value = element.attrib.get(attr_name)
326
- if attr_value is not None:
327
- result.append(attr_value)
328
-
329
- return result if result else default
330
- except Exception:
331
- return default
332
-
333
- def re_search(self, pattern: str, flags: int = re.DOTALL) -> Optional[re.Match]:
334
- """在响应文本上执行正则表达式搜索。"""
335
- if not isinstance(pattern, str):
336
- raise TypeError("Pattern must be a string")
337
- return re.search(pattern, self.text, flags=flags)
338
-
339
- def re_findall(self, pattern: str, flags: int = re.DOTALL) -> List[Any]:
340
- """在响应文本上执行正则表达式查找。"""
341
- if not isinstance(pattern, str):
342
- raise TypeError("Pattern must be a string")
343
- return re.findall(pattern, self.text, flags=flags)
344
-
345
- def get_cookies(self) -> Dict[str, str]:
346
- """从响应头中解析并返回Cookies。"""
347
- cookie_header = self.headers.get("Set-Cookie", "")
348
- if isinstance(cookie_header, list):
349
- cookie_header = ", ".join(cookie_header)
350
- cookies = SimpleCookie()
351
- cookies.load(cookie_header)
352
- return {key: morsel.value for key, morsel in cookies.items()}
353
-
354
- @property
355
- def meta(self) -> Dict:
356
- """获取关联的 Request 对象的 meta 字典。"""
357
- return self.request.meta if self.request else {}
358
-
359
- def __str__(self):
1
+ #!/usr/bin/python
2
+ # -*- coding:UTF-8 -*-
3
+ """
4
+ HTTP Response 封装模块
5
+ =====================
6
+ 提供功能丰富的HTTP响应封装,支持:
7
+ - 智能编码检测和解码
8
+ - XPath/CSS 选择器
9
+ - JSON 解析和缓存
10
+ - 正则表达式支持
11
+ - Cookie 处理
12
+ """
13
+ import re
14
+ import ujson
15
+ from http.cookies import SimpleCookie
16
+ from parsel import Selector, SelectorList
17
+ from typing import Dict, Any, List, Optional, Union
18
+ from urllib.parse import urljoin as _urljoin
19
+
20
+ from crawlo.exceptions import DecodeError
21
+
22
+
23
+ class Response:
24
+ """
25
+ HTTP响应的封装,提供数据解析的便捷方法。
26
+
27
+ 功能特性:
28
+ - 智能编码检测和缓存
29
+ - 懒加载 Selector 实例
30
+ - JSON 解析和缓存
31
+ - 多类型数据提取
32
+ """
33
+
34
+ def __init__(
35
+ self,
36
+ url: str,
37
+ *,
38
+ headers: Dict[str, Any] = None,
39
+ body: bytes = b"",
40
+ method: str = 'GET',
41
+ request: 'Request' = None, # 使用字符串注解避免循环导入
42
+ status_code: int = 200,
43
+ ):
44
+ # 基本属性
45
+ self.url = url
46
+ self.headers = headers or {}
47
+ self.body = body
48
+ self.method = method.upper()
49
+ self.request = request
50
+ self.status_code = status_code
51
+
52
+ # 编码处理
53
+ self.encoding = self._determine_encoding()
54
+
55
+ # 缓存属性
56
+ self._text_cache = None
57
+ self._json_cache = None
58
+ self._selector_instance = None
59
+
60
+ # 状态标记
61
+ self._is_success = 200 <= status_code < 300
62
+ self._is_redirect = 300 <= status_code < 400
63
+ self._is_client_error = 400 <= status_code < 500
64
+ self._is_server_error = status_code >= 500
65
+
66
+ def _determine_encoding(self) -> Optional[str]:
67
+ """智能检测响应编码"""
68
+ # 1. 优先使用 request 的编码
69
+ if self.request and self.request.encoding:
70
+ return self.request.encoding
71
+
72
+ # 2. 从 Content-Type 头中检测
73
+ content_type = self.headers.get("content-type", "") or self.headers.get("Content-Type", "")
74
+ if content_type:
75
+ charset_match = re.search(r"charset=([w-]+)", content_type, re.I)
76
+ if charset_match:
77
+ return charset_match.group(1).lower()
78
+
79
+ # 3. 从 HTML meta 标签中检测(仅对HTML内容)
80
+ if b'<html' in self.body[:1024].lower():
81
+ # 查找 <meta charset="xxx"> 或 <meta http-equiv="Content-Type" content="...charset=xxx">
82
+ html_start = self.body[:4096] # 只检查前4KB
83
+ try:
84
+ html_text = html_start.decode('ascii', errors='ignore')
85
+ # <meta charset="utf-8">
86
+ charset_match = re.search(r'<meta[^>]+charset=["\']?([\w-]+)', html_text, re.I)
87
+ if charset_match:
88
+ return charset_match.group(1).lower()
89
+
90
+ # <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
91
+ content_match = re.search(r'<meta[^>]+content=["\'][^"\'>]*charset=([\w-]+)', html_text, re.I)
92
+ if content_match:
93
+ return content_match.group(1).lower()
94
+ except Exception:
95
+ pass
96
+
97
+ # 4. 默认使用 utf-8
98
+ return 'utf-8'
99
+
100
+ @property
101
+ def text(self) -> str:
102
+ """将响应体(body)以正确的编码解码为字符串,并缓存结果。"""
103
+ if self._text_cache is not None:
104
+ return self._text_cache
105
+
106
+ if not self.body:
107
+ self._text_cache = ""
108
+ return self._text_cache
109
+
110
+ # 尝试多种编码
111
+ encodings_to_try = [self.encoding]
112
+ if self.encoding != 'utf-8':
113
+ encodings_to_try.append('utf-8')
114
+ if 'gbk' not in encodings_to_try:
115
+ encodings_to_try.append('gbk')
116
+ if 'gb2312' not in encodings_to_try:
117
+ encodings_to_try.append('gb2312')
118
+ encodings_to_try.append('latin1') # 最后的回退选项
119
+
120
+ for encoding in encodings_to_try:
121
+ if not encoding:
122
+ continue
123
+ try:
124
+ self._text_cache = self.body.decode(encoding)
125
+ return self._text_cache
126
+ except (UnicodeDecodeError, LookupError):
127
+ continue
128
+
129
+ # 所有编码都失败,使用容错解码
130
+ try:
131
+ self._text_cache = self.body.decode('utf-8', errors='replace')
132
+ return self._text_cache
133
+ except Exception as e:
134
+ raise DecodeError(f"Failed to decode response from {self.url}: {e}")
135
+
136
+ @property
137
+ def is_success(self) -> bool:
138
+ """检查响应是否成功 (2xx)"""
139
+ return self._is_success
140
+
141
+ @property
142
+ def is_redirect(self) -> bool:
143
+ """检查响应是否为重定向 (3xx)"""
144
+ return self._is_redirect
145
+
146
+ @property
147
+ def is_client_error(self) -> bool:
148
+ """检查响应是否为客户端错误 (4xx)"""
149
+ return self._is_client_error
150
+
151
+ @property
152
+ def is_server_error(self) -> bool:
153
+ """检查响应是否为服务器错误 (5xx)"""
154
+ return self._is_server_error
155
+
156
+ @property
157
+ def content_type(self) -> str:
158
+ """获取响应的 Content-Type"""
159
+ return self.headers.get('content-type', '') or self.headers.get('Content-Type', '')
160
+
161
+ @property
162
+ def content_length(self) -> Optional[int]:
163
+ """获取响应的 Content-Length"""
164
+ length = self.headers.get('content-length') or self.headers.get('Content-Length')
165
+ return int(length) if length else None
166
+
167
+ def json(self, default: Any = None) -> Any:
168
+ """将响应文本解析为 JSON 对象。"""
169
+ if self._json_cache is not None:
170
+ return self._json_cache
171
+
172
+ try:
173
+ self._json_cache = ujson.loads(self.text)
174
+ return self._json_cache
175
+ except (ujson.JSONDecodeError, ValueError) as e:
176
+ if default is not None:
177
+ return default
178
+ raise DecodeError(f"Failed to parse JSON from {self.url}: {e}")
179
+
180
+ def urljoin(self, url: str) -> str:
181
+ """拼接 URL,自动处理相对路径。"""
182
+ return _urljoin(self.url, url)
183
+
184
+ @property
185
+ def _selector(self) -> Selector:
186
+ """懒加载 Selector 实例"""
187
+ if self._selector_instance is None:
188
+ self._selector_instance = Selector(self.text)
189
+ return self._selector_instance
190
+
191
+ def xpath(self, query: str) -> SelectorList:
192
+ """使用 XPath 选择器查询文档。"""
193
+ return self._selector.xpath(query)
194
+
195
+ def css(self, query: str) -> SelectorList:
196
+ """使用 CSS 选择器查询文档。"""
197
+ return self._selector.css(query)
198
+
199
+ def _is_xpath(self, query: str) -> bool:
200
+ """判断查询语句是否为XPath"""
201
+ return query.startswith(('/', '//', './'))
202
+
203
+ def _extract_text_from_elements(self, elements: SelectorList, join_str: str = " ") -> str:
204
+ """
205
+ 从元素列表中提取文本并拼接
206
+
207
+ :param elements: SelectorList元素列表
208
+ :param join_str: 文本拼接分隔符
209
+ :return: 拼接后的文本
210
+ """
211
+ texts = []
212
+ for element in elements:
213
+ # 获取元素的所有文本节点
214
+ if hasattr(element, 'xpath'):
215
+ element_texts = element.xpath('.//text()').getall()
216
+ else:
217
+ element_texts = [str(element)]
218
+ # 清理并添加非空文本
219
+ for text in element_texts:
220
+ cleaned = text.strip()
221
+ if cleaned:
222
+ texts.append(cleaned)
223
+ return join_str.join(texts)
224
+
225
+ def extract_text(self, xpath_or_css: str, join_str: str = " ", default: str = '') -> str:
226
+ """
227
+ 提取单个元素的文本内容,支持CSS和XPath选择器
228
+
229
+ 参数:
230
+ xpath_or_css: XPath或CSS选择器
231
+ join_str: 文本拼接分隔符(默认为空格)
232
+ default: 默认返回值,当未找到元素时返回
233
+
234
+ 返回:
235
+ 拼接后的纯文本字符串
236
+ """
237
+ try:
238
+ elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
239
+ if not elements:
240
+ return default
241
+ return self._extract_text_from_elements(elements, join_str)
242
+ except Exception:
243
+ return default
244
+
245
+ def extract_texts(self, xpath_or_css: str, join_str: str = " ", default: List[str] = None) -> List[str]:
246
+ """
247
+ 提取多个元素的文本内容列表,支持CSS和XPath选择器
248
+
249
+ 参数:
250
+ xpath_or_css: XPath或CSS选择器
251
+ join_str: 单个节点内文本拼接分隔符
252
+ default: 默认返回值,当未找到元素时返回
253
+
254
+ 返回:
255
+ 纯文本列表(每个元素对应一个节点的文本)
256
+ """
257
+ if default is None:
258
+ default = []
259
+
260
+ try:
261
+ elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
262
+ if not elements:
263
+ return default
264
+
265
+ result = []
266
+ for element in elements:
267
+ # 对每个元素提取文本
268
+ if hasattr(element, 'xpath'):
269
+ texts = element.xpath('.//text()').getall()
270
+ else:
271
+ texts = [str(element)]
272
+
273
+ # 清理文本并拼接
274
+ clean_texts = [text.strip() for text in texts if text.strip()]
275
+ if clean_texts:
276
+ result.append(join_str.join(clean_texts))
277
+
278
+ return result if result else default
279
+ except Exception:
280
+ return default
281
+
282
+ def extract_attr(self, xpath_or_css: str, attr_name: str, default: Any = None) -> Any:
283
+ """
284
+ 提取单个元素的属性值,支持CSS和XPath选择器
285
+
286
+ 参数:
287
+ xpath_or_css: XPath或CSS选择器
288
+ attr_name: 属性名称
289
+ default: 默认返回值
290
+
291
+ 返回:
292
+ 属性值或默认值
293
+ """
294
+ try:
295
+ elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
296
+ if not elements:
297
+ return default
298
+ return elements.attrib.get(attr_name, default)
299
+ except Exception:
300
+ return default
301
+
302
+ def extract_attrs(self, xpath_or_css: str, attr_name: str, default: List[Any] = None) -> List[Any]:
303
+ """
304
+ 提取多个元素的属性值列表,支持CSS和XPath选择器
305
+
306
+ 参数:
307
+ xpath_or_css: XPath或CSS选择器
308
+ attr_name: 属性名称
309
+ default: 默认返回值
310
+
311
+ 返回:
312
+ 属性值列表
313
+ """
314
+ if default is None:
315
+ default = []
316
+
317
+ try:
318
+ elements = self.xpath(xpath_or_css) if self._is_xpath(xpath_or_css) else self.css(xpath_or_css)
319
+ if not elements:
320
+ return default
321
+
322
+ result = []
323
+ for element in elements:
324
+ if hasattr(element, 'attrib'):
325
+ attr_value = element.attrib.get(attr_name)
326
+ if attr_value is not None:
327
+ result.append(attr_value)
328
+
329
+ return result if result else default
330
+ except Exception:
331
+ return default
332
+
333
+ def re_search(self, pattern: str, flags: int = re.DOTALL) -> Optional[re.Match]:
334
+ """在响应文本上执行正则表达式搜索。"""
335
+ if not isinstance(pattern, str):
336
+ raise TypeError("Pattern must be a string")
337
+ return re.search(pattern, self.text, flags=flags)
338
+
339
+ def re_findall(self, pattern: str, flags: int = re.DOTALL) -> List[Any]:
340
+ """在响应文本上执行正则表达式查找。"""
341
+ if not isinstance(pattern, str):
342
+ raise TypeError("Pattern must be a string")
343
+ return re.findall(pattern, self.text, flags=flags)
344
+
345
+ def get_cookies(self) -> Dict[str, str]:
346
+ """从响应头中解析并返回Cookies。"""
347
+ cookie_header = self.headers.get("Set-Cookie", "")
348
+ if isinstance(cookie_header, list):
349
+ cookie_header = ", ".join(cookie_header)
350
+ cookies = SimpleCookie()
351
+ cookies.load(cookie_header)
352
+ return {key: morsel.value for key, morsel in cookies.items()}
353
+
354
+ @property
355
+ def meta(self) -> Dict:
356
+ """获取关联的 Request 对象的 meta 字典。"""
357
+ return self.request.meta if self.request else {}
358
+
359
+ def __str__(self):
360
360
  return f"<{self.status_code} {self.url}>"