crawlo 1.0.4__py3-none-any.whl → 1.0.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.
- crawlo/__init__.py +25 -9
- crawlo/__version__.py +1 -1
- crawlo/core/__init__.py +2 -2
- crawlo/core/engine.py +158 -158
- crawlo/core/processor.py +40 -40
- crawlo/core/scheduler.py +57 -57
- crawlo/crawler.py +424 -242
- crawlo/downloader/__init__.py +78 -78
- crawlo/downloader/aiohttp_downloader.py +200 -259
- crawlo/downloader/cffi_downloader.py +277 -0
- crawlo/downloader/httpx_downloader.py +246 -187
- crawlo/event.py +11 -11
- crawlo/exceptions.py +73 -64
- crawlo/extension/__init__.py +31 -31
- crawlo/extension/log_interval.py +49 -49
- crawlo/extension/log_stats.py +44 -44
- crawlo/extension/logging_extension.py +35 -0
- crawlo/filters/__init__.py +37 -37
- crawlo/filters/aioredis_filter.py +150 -150
- crawlo/filters/memory_filter.py +202 -202
- crawlo/items/__init__.py +62 -62
- crawlo/items/items.py +115 -119
- crawlo/middleware/__init__.py +21 -21
- crawlo/middleware/default_header.py +32 -32
- crawlo/middleware/download_delay.py +28 -28
- crawlo/middleware/middleware_manager.py +135 -140
- crawlo/middleware/proxy.py +246 -0
- crawlo/middleware/request_ignore.py +30 -30
- crawlo/middleware/response_code.py +18 -18
- crawlo/middleware/response_filter.py +26 -26
- crawlo/middleware/retry.py +90 -90
- crawlo/network/__init__.py +7 -7
- crawlo/network/request.py +203 -204
- crawlo/network/response.py +166 -166
- crawlo/pipelines/__init__.py +13 -13
- crawlo/pipelines/console_pipeline.py +39 -39
- crawlo/pipelines/mongo_pipeline.py +116 -116
- crawlo/pipelines/mysql_batch_pipline.py +273 -134
- crawlo/pipelines/mysql_pipeline.py +195 -195
- crawlo/pipelines/pipeline_manager.py +56 -56
- crawlo/settings/__init__.py +7 -7
- crawlo/settings/default_settings.py +169 -94
- crawlo/settings/setting_manager.py +99 -99
- crawlo/spider/__init__.py +41 -36
- crawlo/stats_collector.py +59 -59
- crawlo/subscriber.py +106 -106
- crawlo/task_manager.py +27 -27
- crawlo/templates/item_template.tmpl +21 -21
- crawlo/templates/project_template/main.py +32 -32
- crawlo/templates/project_template/setting.py +189 -189
- crawlo/templates/spider_template.tmpl +30 -30
- crawlo/utils/__init__.py +7 -7
- crawlo/utils/concurrency_manager.py +124 -124
- crawlo/utils/date_tools.py +233 -177
- crawlo/utils/db_helper.py +344 -0
- crawlo/utils/func_tools.py +82 -82
- crawlo/utils/log.py +129 -39
- crawlo/utils/pqueue.py +173 -173
- crawlo/utils/project.py +59 -59
- crawlo/utils/request.py +267 -122
- crawlo/utils/system.py +11 -11
- crawlo/utils/tools.py +5 -303
- crawlo/utils/url.py +39 -39
- {crawlo-1.0.4.dist-info → crawlo-1.0.5.dist-info}/METADATA +49 -48
- crawlo-1.0.5.dist-info/RECORD +84 -0
- {crawlo-1.0.4.dist-info → crawlo-1.0.5.dist-info}/top_level.txt +1 -0
- examples/__init__.py +0 -0
- examples/gxb/__init__.py +0 -0
- examples/gxb/items.py +36 -0
- examples/gxb/run.py +15 -0
- examples/gxb/settings.py +71 -0
- examples/gxb/spider/__init__.py +0 -0
- examples/gxb/spider/miit_spider.py +180 -0
- examples/gxb/spider/telecom_device_licenses.py +129 -0
- tests/__init__.py +7 -7
- tests/test_proxy_health_check.py +33 -0
- tests/test_proxy_middleware_integration.py +137 -0
- tests/test_proxy_providers.py +57 -0
- tests/test_proxy_stats.py +20 -0
- tests/test_proxy_strategies.py +60 -0
- crawlo/downloader/playwright_downloader.py +0 -161
- crawlo-1.0.4.dist-info/RECORD +0 -79
- tests/baidu_spider/__init__.py +0 -7
- tests/baidu_spider/demo.py +0 -94
- tests/baidu_spider/items.py +0 -25
- tests/baidu_spider/middleware.py +0 -49
- tests/baidu_spider/pipeline.py +0 -55
- tests/baidu_spider/request_fingerprints.txt +0 -9
- tests/baidu_spider/run.py +0 -27
- tests/baidu_spider/settings.py +0 -80
- tests/baidu_spider/spiders/__init__.py +0 -7
- tests/baidu_spider/spiders/bai_du.py +0 -61
- tests/baidu_spider/spiders/sina.py +0 -79
- {crawlo-1.0.4.dist-info → crawlo-1.0.5.dist-info}/WHEEL +0 -0
- {crawlo-1.0.4.dist-info → crawlo-1.0.5.dist-info}/entry_points.txt +0 -0
crawlo/network/request.py
CHANGED
|
@@ -1,205 +1,204 @@
|
|
|
1
|
-
#!/usr/bin/python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
import json
|
|
4
|
-
from copy import deepcopy
|
|
5
|
-
from urllib.parse import urlencode
|
|
6
|
-
from w3lib.url import safe_url_string
|
|
7
|
-
from typing import Dict, Optional, Callable, Union, Any, TypeVar, List
|
|
8
|
-
|
|
9
|
-
from crawlo.utils.url import escape_ajax
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
_Request = TypeVar("_Request", bound="Request")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class RequestPriority:
|
|
16
|
-
"""请求优先级常量"""
|
|
17
|
-
HIGH = -100
|
|
18
|
-
NORMAL = 0
|
|
19
|
-
LOW = 100
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class Request:
|
|
23
|
-
"""
|
|
24
|
-
封装一个 HTTP 请求对象,用于爬虫框架中表示一个待抓取的请求任务。
|
|
25
|
-
支持 JSON、表单、原始 body 提交,自动处理 Content-Type 与编码。
|
|
26
|
-
不支持文件上传(multipart/form-data),保持轻量。
|
|
27
|
-
"""
|
|
28
|
-
|
|
29
|
-
__slots__ = (
|
|
30
|
-
'_url',
|
|
31
|
-
'_meta',
|
|
32
|
-
'callback',
|
|
33
|
-
'cb_kwargs',
|
|
34
|
-
'err_back',
|
|
35
|
-
'headers',
|
|
36
|
-
'body',
|
|
37
|
-
'method',
|
|
38
|
-
'cookies',
|
|
39
|
-
'priority',
|
|
40
|
-
'encoding',
|
|
41
|
-
'dont_filter',
|
|
42
|
-
'timeout',
|
|
43
|
-
'proxy',
|
|
44
|
-
'allow_redirects',
|
|
45
|
-
'auth',
|
|
46
|
-
'verify',
|
|
47
|
-
'flags',
|
|
48
|
-
|
|
49
|
-
'
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
:param
|
|
79
|
-
:param
|
|
80
|
-
:param
|
|
81
|
-
:param
|
|
82
|
-
:param
|
|
83
|
-
:param
|
|
84
|
-
:param
|
|
85
|
-
:param
|
|
86
|
-
:param
|
|
87
|
-
:param
|
|
88
|
-
:param
|
|
89
|
-
:param
|
|
90
|
-
:param
|
|
91
|
-
:param
|
|
92
|
-
:param
|
|
93
|
-
:param
|
|
94
|
-
:param
|
|
95
|
-
:param
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
self.
|
|
99
|
-
self.
|
|
100
|
-
self.
|
|
101
|
-
self.
|
|
102
|
-
self.
|
|
103
|
-
self.
|
|
104
|
-
self.
|
|
105
|
-
self.
|
|
106
|
-
self.
|
|
107
|
-
self.
|
|
108
|
-
self.
|
|
109
|
-
self.
|
|
110
|
-
self.
|
|
111
|
-
self.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
self.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
self.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
self.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
"""用于按优先级排序"""
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
import json
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
from urllib.parse import urlencode
|
|
6
|
+
from w3lib.url import safe_url_string
|
|
7
|
+
from typing import Dict, Optional, Callable, Union, Any, TypeVar, List
|
|
8
|
+
|
|
9
|
+
from crawlo.utils.url import escape_ajax
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
_Request = TypeVar("_Request", bound="Request")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RequestPriority:
|
|
16
|
+
"""请求优先级常量"""
|
|
17
|
+
HIGH = -100
|
|
18
|
+
NORMAL = 0
|
|
19
|
+
LOW = 100
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Request:
|
|
23
|
+
"""
|
|
24
|
+
封装一个 HTTP 请求对象,用于爬虫框架中表示一个待抓取的请求任务。
|
|
25
|
+
支持 JSON、表单、原始 body 提交,自动处理 Content-Type 与编码。
|
|
26
|
+
不支持文件上传(multipart/form-data),保持轻量。
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
__slots__ = (
|
|
30
|
+
'_url',
|
|
31
|
+
'_meta',
|
|
32
|
+
'callback',
|
|
33
|
+
'cb_kwargs',
|
|
34
|
+
'err_back',
|
|
35
|
+
'headers',
|
|
36
|
+
'body',
|
|
37
|
+
'method',
|
|
38
|
+
'cookies',
|
|
39
|
+
'priority',
|
|
40
|
+
'encoding',
|
|
41
|
+
'dont_filter',
|
|
42
|
+
'timeout',
|
|
43
|
+
'proxy',
|
|
44
|
+
'allow_redirects',
|
|
45
|
+
'auth',
|
|
46
|
+
'verify',
|
|
47
|
+
'flags',
|
|
48
|
+
'_json_body',
|
|
49
|
+
'_form_data'
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
url: str,
|
|
55
|
+
callback: Optional[Callable] = None,
|
|
56
|
+
method: Optional[str] = 'GET',
|
|
57
|
+
headers: Optional[Dict[str, str]] = None,
|
|
58
|
+
body: Optional[Union[bytes, str, Dict[Any, Any]]] = None,
|
|
59
|
+
form_data: Optional[Dict[Any, Any]] = None,
|
|
60
|
+
json_body: Optional[Dict[Any, Any]] = None,
|
|
61
|
+
cb_kwargs: Optional[Dict[str, Any]] = None,
|
|
62
|
+
cookies: Optional[Dict[str, str]] = None,
|
|
63
|
+
meta: Optional[Dict[str, Any]] = None,
|
|
64
|
+
priority: int = RequestPriority.NORMAL,
|
|
65
|
+
dont_filter: bool = False,
|
|
66
|
+
timeout: Optional[float] = None,
|
|
67
|
+
proxy: Optional[str] = None,
|
|
68
|
+
allow_redirects: bool = True,
|
|
69
|
+
auth: Optional[tuple] = None,
|
|
70
|
+
verify: bool = True,
|
|
71
|
+
flags: Optional[List[str]] = None,
|
|
72
|
+
encoding: str = 'utf-8'
|
|
73
|
+
):
|
|
74
|
+
"""
|
|
75
|
+
初始化请求对象。
|
|
76
|
+
|
|
77
|
+
:param url: 请求 URL(必须)
|
|
78
|
+
:param callback: 成功回调函数
|
|
79
|
+
:param method: HTTP 方法,默认 GET
|
|
80
|
+
:param headers: 请求头
|
|
81
|
+
:param body: 原始请求体(bytes/str),若为 dict 且未使用 json_body/form_data,则自动转为 JSON
|
|
82
|
+
:param form_data: 表单数据,自动转为 application/x-www-form-urlencoded
|
|
83
|
+
:param json_body: JSON 数据,自动序列化并设置 Content-Type
|
|
84
|
+
:param cb_kwargs: 传递给 callback 的额外参数
|
|
85
|
+
:param cookies: Cookies 字典
|
|
86
|
+
:param meta: 元数据(跨中间件传递数据)
|
|
87
|
+
:param priority: 优先级(数值越小越优先)
|
|
88
|
+
:param dont_filter: 是否跳过去重
|
|
89
|
+
:param timeout: 超时时间(秒)
|
|
90
|
+
:param proxy: 代理地址,如 http://127.0.0.1:8080
|
|
91
|
+
:param allow_redirects: 是否允许重定向
|
|
92
|
+
:param auth: 认证元组 (username, password)
|
|
93
|
+
:param verify: 是否验证 SSL 证书
|
|
94
|
+
:param flags: 标记(用于调试或分类)
|
|
95
|
+
:param encoding: 字符编码,默认 utf-8
|
|
96
|
+
"""
|
|
97
|
+
self.callback = callback
|
|
98
|
+
self.method = str(method).upper()
|
|
99
|
+
self.headers = headers or {}
|
|
100
|
+
self.cookies = cookies or {}
|
|
101
|
+
self.priority = -priority # 用于排序:值越小优先级越高
|
|
102
|
+
self._meta = deepcopy(meta) if meta is not None else {}
|
|
103
|
+
self.timeout = self._meta.get('download_timeout', timeout)
|
|
104
|
+
self.proxy = proxy
|
|
105
|
+
self.allow_redirects = allow_redirects
|
|
106
|
+
self.auth = auth
|
|
107
|
+
self.verify = verify
|
|
108
|
+
self.flags = flags or []
|
|
109
|
+
self.encoding = encoding
|
|
110
|
+
self.cb_kwargs = cb_kwargs or {}
|
|
111
|
+
self.body = body
|
|
112
|
+
# 保存高层语义参数(用于 copy)
|
|
113
|
+
self._json_body = json_body
|
|
114
|
+
self._form_data = form_data
|
|
115
|
+
|
|
116
|
+
# 构建 body
|
|
117
|
+
if json_body is not None:
|
|
118
|
+
if 'Content-Type' not in self.headers:
|
|
119
|
+
self.headers['Content-Type'] = 'application/json'
|
|
120
|
+
self.body = json.dumps(json_body, ensure_ascii=False).encode(encoding)
|
|
121
|
+
if self.method == 'GET':
|
|
122
|
+
self.method = 'POST'
|
|
123
|
+
|
|
124
|
+
elif form_data is not None:
|
|
125
|
+
if self.method == 'GET':
|
|
126
|
+
self.method = 'POST'
|
|
127
|
+
if 'Content-Type' not in self.headers:
|
|
128
|
+
self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
129
|
+
query_str = urlencode(form_data)
|
|
130
|
+
self.body = query_str.encode(encoding) # ✅ 显式编码为 bytes
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
else:
|
|
134
|
+
# 处理原始 body
|
|
135
|
+
if isinstance(self.body, dict):
|
|
136
|
+
if 'Content-Type' not in self.headers:
|
|
137
|
+
self.headers['Content-Type'] = 'application/json'
|
|
138
|
+
self.body = json.dumps(self.body, ensure_ascii=False).encode(encoding)
|
|
139
|
+
elif isinstance(self.body, str):
|
|
140
|
+
self.body = self.body.encode(encoding)
|
|
141
|
+
|
|
142
|
+
self.dont_filter = dont_filter
|
|
143
|
+
self._set_url(url)
|
|
144
|
+
|
|
145
|
+
def copy(self: _Request) -> _Request:
|
|
146
|
+
"""
|
|
147
|
+
创建当前请求的副本,保留所有高层语义(json_body/form_data)。
|
|
148
|
+
"""
|
|
149
|
+
return type(self)(
|
|
150
|
+
url=self.url,
|
|
151
|
+
callback=self.callback,
|
|
152
|
+
method=self.method,
|
|
153
|
+
headers=self.headers.copy(),
|
|
154
|
+
body=None, # 由 form_data/json_body 重新生成
|
|
155
|
+
form_data=self._form_data,
|
|
156
|
+
json_body=self._json_body,
|
|
157
|
+
cb_kwargs=deepcopy(self.cb_kwargs),
|
|
158
|
+
err_back=self.err_back,
|
|
159
|
+
cookies=self.cookies.copy(),
|
|
160
|
+
meta=deepcopy(self._meta),
|
|
161
|
+
priority=-self.priority,
|
|
162
|
+
dont_filter=self.dont_filter,
|
|
163
|
+
timeout=self.timeout,
|
|
164
|
+
proxy=self.proxy,
|
|
165
|
+
allow_redirects=self.allow_redirects,
|
|
166
|
+
auth=self.auth,
|
|
167
|
+
verify=self.verify,
|
|
168
|
+
flags=self.flags.copy(),
|
|
169
|
+
encoding=self.encoding
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
def set_meta(self, key: str, value: Any) -> None:
|
|
173
|
+
"""设置 meta 中的某个键值。"""
|
|
174
|
+
self._meta[key] = value
|
|
175
|
+
|
|
176
|
+
def _set_url(self, url: str) -> None:
|
|
177
|
+
"""安全设置 URL,确保格式正确。"""
|
|
178
|
+
if not isinstance(url, str):
|
|
179
|
+
raise TypeError(f"Request url 必须为字符串,当前类型: {type(url).__name__}")
|
|
180
|
+
|
|
181
|
+
s = safe_url_string(url, self.encoding)
|
|
182
|
+
escaped_url = escape_ajax(s)
|
|
183
|
+
self._url = escaped_url
|
|
184
|
+
|
|
185
|
+
if not self._url.startswith(('http://', 'https://')):
|
|
186
|
+
raise ValueError(f"URL 缺少 scheme: {self._url}")
|
|
187
|
+
|
|
188
|
+
@property
|
|
189
|
+
def url(self) -> str:
|
|
190
|
+
return self._url
|
|
191
|
+
|
|
192
|
+
@property
|
|
193
|
+
def meta(self) -> Dict[str, Any]:
|
|
194
|
+
return self._meta
|
|
195
|
+
|
|
196
|
+
def __str__(self) -> str:
|
|
197
|
+
return f'<Request url={self.url} method={self.method}>'
|
|
198
|
+
|
|
199
|
+
def __repr__(self) -> str:
|
|
200
|
+
return str(self)
|
|
201
|
+
|
|
202
|
+
def __lt__(self, other: _Request) -> bool:
|
|
203
|
+
"""用于按优先级排序"""
|
|
205
204
|
return self.priority < other.priority
|