crawlo 1.0.1__py3-none-any.whl → 1.0.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.
- crawlo/__init__.py +9 -6
- crawlo/__version__.py +1 -2
- crawlo/core/__init__.py +2 -2
- crawlo/core/engine.py +158 -158
- crawlo/core/processor.py +40 -40
- crawlo/core/scheduler.py +57 -59
- crawlo/crawler.py +242 -107
- crawlo/downloader/__init__.py +78 -78
- crawlo/downloader/aiohttp_downloader.py +259 -96
- crawlo/downloader/httpx_downloader.py +187 -48
- crawlo/downloader/playwright_downloader.py +160 -160
- crawlo/event.py +11 -11
- crawlo/exceptions.py +64 -64
- crawlo/extension/__init__.py +31 -31
- crawlo/extension/log_interval.py +49 -49
- crawlo/extension/log_stats.py +44 -44
- crawlo/filters/__init__.py +37 -37
- crawlo/filters/aioredis_filter.py +157 -129
- crawlo/filters/memory_filter.py +202 -203
- crawlo/filters/redis_filter.py +119 -119
- crawlo/items/__init__.py +62 -62
- crawlo/items/items.py +118 -118
- 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 +140 -140
- 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 -89
- crawlo/network/__init__.py +7 -7
- crawlo/network/request.py +205 -155
- crawlo/network/response.py +166 -93
- 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 +133 -133
- crawlo/pipelines/mysql_pipeline.py +195 -176
- crawlo/pipelines/pipeline_manager.py +56 -56
- crawlo/settings/__init__.py +7 -7
- crawlo/settings/default_settings.py +93 -89
- crawlo/settings/setting_manager.py +99 -99
- crawlo/spider/__init__.py +36 -36
- crawlo/stats_collector.py +59 -47
- crawlo/subscriber.py +106 -27
- 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 +125 -0
- crawlo/utils/date_tools.py +177 -177
- crawlo/utils/func_tools.py +82 -82
- crawlo/utils/log.py +39 -39
- crawlo/utils/pqueue.py +173 -173
- crawlo/utils/project.py +59 -59
- crawlo/utils/request.py +122 -85
- crawlo/utils/system.py +11 -11
- crawlo/utils/tools.py +303 -0
- crawlo/utils/url.py +39 -39
- {crawlo-1.0.1.dist-info → crawlo-1.0.3.dist-info}/METADATA +48 -36
- crawlo-1.0.3.dist-info/RECORD +80 -0
- {crawlo-1.0.1.dist-info → crawlo-1.0.3.dist-info}/top_level.txt +1 -0
- tests/__init__.py +7 -0
- tests/baidu_spider/__init__.py +7 -0
- tests/baidu_spider/demo.py +94 -0
- tests/baidu_spider/items.py +25 -0
- tests/baidu_spider/middleware.py +49 -0
- tests/baidu_spider/pipeline.py +55 -0
- tests/baidu_spider/request_fingerprints.txt +9 -0
- tests/baidu_spider/run.py +27 -0
- tests/baidu_spider/settings.py +78 -0
- tests/baidu_spider/spiders/__init__.py +7 -0
- tests/baidu_spider/spiders/bai_du.py +61 -0
- tests/baidu_spider/spiders/sina.py +79 -0
- crawlo-1.0.1.dist-info/RECORD +0 -67
- crawlo-1.0.1.dist-info/licenses/LICENSE +0 -23
- {crawlo-1.0.1.dist-info → crawlo-1.0.3.dist-info}/WHEEL +0 -0
- {crawlo-1.0.1.dist-info → crawlo-1.0.3.dist-info}/entry_points.txt +0 -0
crawlo/utils/date_tools.py
CHANGED
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
#!/usr/bin/python
|
|
2
|
-
# -*- coding:UTF-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
# @Time : 2025-05-17 10:20
|
|
5
|
-
# @Author : crawl-coder
|
|
6
|
-
# @Desc : 时间工具
|
|
7
|
-
"""
|
|
8
|
-
import dateparser
|
|
9
|
-
from typing import Optional, Union
|
|
10
|
-
from datetime import datetime, timedelta
|
|
11
|
-
from dateutil.relativedelta import relativedelta
|
|
12
|
-
|
|
13
|
-
# 常见时间格式列表
|
|
14
|
-
COMMON_FORMATS = [
|
|
15
|
-
"%Y-%m-%d %H:%M:%S",
|
|
16
|
-
"%Y/%m/%d %H:%M:%S",
|
|
17
|
-
"%d-%m-%Y %H:%M:%S",
|
|
18
|
-
"%d/%m/%Y %H:%M:%S",
|
|
19
|
-
"%Y-%m-%d",
|
|
20
|
-
"%Y/%m/%d",
|
|
21
|
-
"%d-%m-%Y",
|
|
22
|
-
"%d/%m/%Y",
|
|
23
|
-
"%b %d, %Y", # Jan 01, 2023
|
|
24
|
-
"%B %d, %Y", # January 01, 2023
|
|
25
|
-
"%Y年%m月%d日", # 2023年01月01日
|
|
26
|
-
"%Y年%m月%d日 %H时%M分%S秒", # 2023年01月01日 12时30分45秒
|
|
27
|
-
"%a %b %d %H:%M:%S %Y", # Wed Jan 01 12:00:00 2020
|
|
28
|
-
"%a, %d %b %Y %H:%M:%S", # Wed, 01 Jan 2020 12:00:00
|
|
29
|
-
"%Y-%m-%dT%H:%M:%S.%f", # ✅ 新增:ISO 8601 格式(带毫秒)
|
|
30
|
-
]
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def normalize_time(time_str: str) -> Optional[datetime]:
|
|
34
|
-
"""
|
|
35
|
-
尝试使用常见格式解析时间字符串。
|
|
36
|
-
|
|
37
|
-
:param time_str: 时间字符串(如 "2023-01-01 12:00:00")
|
|
38
|
-
:return: 解析成功返回 datetime 对象,失败返回 None 或抛出异常(可选)
|
|
39
|
-
"""
|
|
40
|
-
for fmt in COMMON_FORMATS:
|
|
41
|
-
try:
|
|
42
|
-
return datetime.strptime(time_str, fmt)
|
|
43
|
-
except ValueError:
|
|
44
|
-
continue
|
|
45
|
-
raise ValueError(f"无法解析时间字符串:{time_str}")
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def get_current_time(fmt: str = '%Y-%m-%d %H:%M:%S'):
|
|
49
|
-
"""
|
|
50
|
-
获取当前时间,根据是否传入格式化参数决定返回类型
|
|
51
|
-
:param fmt: 格式化字符串,如 "%Y-%m-%d %H:%M:%S"
|
|
52
|
-
:return: datetime 或 str
|
|
53
|
-
"""
|
|
54
|
-
dt = datetime.now()
|
|
55
|
-
if fmt is not None:
|
|
56
|
-
return dt.strftime(fmt)
|
|
57
|
-
return dt
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def time_diff_seconds(start_time: str, end_time: str, fmt: str = '%Y-%m-%d %H:%M:%S'):
|
|
61
|
-
"""
|
|
62
|
-
计算两个时间字符串之间的秒数差。
|
|
63
|
-
|
|
64
|
-
:param start_time: 起始时间字符串
|
|
65
|
-
:param end_time: 结束时间字符串
|
|
66
|
-
:param fmt: 时间格式,默认为 '%Y-%m-%d %H:%M:%S'
|
|
67
|
-
:return: 秒数差(总是正整数)
|
|
68
|
-
"""
|
|
69
|
-
start = datetime.strptime(start_time, fmt)
|
|
70
|
-
end = datetime.strptime(end_time, fmt)
|
|
71
|
-
delta = end - start
|
|
72
|
-
return int(delta.total_seconds())
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
TimeType = Union[str, datetime]
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def time_diff(start: TimeType, end: TimeType, fmt: str = None, unit='seconds', auto_parse=True) -> Optional[int]:
|
|
79
|
-
"""
|
|
80
|
-
计算两个时间之间的差值(支持字符串或 datetime)。
|
|
81
|
-
|
|
82
|
-
:param start: 起始时间(字符串或 datetime)
|
|
83
|
-
:param end: 结束时间(字符串或 datetime)
|
|
84
|
-
:param fmt: 时间格式(如果传入字符串且 auto_parse=False 时需要)
|
|
85
|
-
:param unit: 单位(seconds, minutes, hours, days)
|
|
86
|
-
:param auto_parse: 是否自动尝试解析任意格式的字符串(推荐开启)
|
|
87
|
-
:return: 差值整数(根据 unit 返回),失败返回 None
|
|
88
|
-
"""
|
|
89
|
-
|
|
90
|
-
def ensure_datetime(t):
|
|
91
|
-
if isinstance(t, datetime):
|
|
92
|
-
return t
|
|
93
|
-
elif isinstance(t, str):
|
|
94
|
-
if auto_parse:
|
|
95
|
-
parsed = normalize_time(t)
|
|
96
|
-
if parsed:
|
|
97
|
-
return parsed
|
|
98
|
-
if fmt:
|
|
99
|
-
return datetime.strptime(t, fmt)
|
|
100
|
-
raise ValueError("字符串时间未提供格式,或无法自动解析")
|
|
101
|
-
else:
|
|
102
|
-
raise TypeError(f"不支持的时间类型: {type(t)}")
|
|
103
|
-
|
|
104
|
-
start_dt = ensure_datetime(start)
|
|
105
|
-
end_dt = ensure_datetime(end)
|
|
106
|
-
|
|
107
|
-
delta = (end_dt - start_dt)
|
|
108
|
-
abs_seconds = int(abs(delta.total_seconds()))
|
|
109
|
-
|
|
110
|
-
if unit == 'seconds':
|
|
111
|
-
return abs_seconds
|
|
112
|
-
elif unit == 'minutes':
|
|
113
|
-
return abs_seconds // 60
|
|
114
|
-
elif unit == 'hours':
|
|
115
|
-
return abs_seconds // 3600
|
|
116
|
-
elif unit == 'days':
|
|
117
|
-
return abs_seconds // 86400
|
|
118
|
-
else:
|
|
119
|
-
raise ValueError(f"Unsupported unit: {unit}")
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
def format_datetime(dt, fmt="%Y-%m-%d %H:%M:%S"):
|
|
123
|
-
"""格式化时间"""
|
|
124
|
-
return dt.strftime(fmt)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
def parse_datetime(s, fmt="%Y-%m-%d %H:%M:%S"):
|
|
128
|
-
"""将字符串解析为 datetime 对象"""
|
|
129
|
-
return datetime.strptime(s, fmt)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
def datetime_to_timestamp(dt):
|
|
133
|
-
"""将 datetime 转换为时间戳"""
|
|
134
|
-
return dt.timestamp()
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def timestamp_to_datetime(ts):
|
|
138
|
-
"""将时间戳转换为 datetime"""
|
|
139
|
-
return datetime.fromtimestamp(ts)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
def add_days(dt, days=0):
|
|
143
|
-
"""日期加减(天)"""
|
|
144
|
-
return dt + timedelta(days=days)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def add_months(dt, months=0):
|
|
148
|
-
"""日期加减(月)"""
|
|
149
|
-
return dt + relativedelta(months=months)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
def days_between(dt1, dt2):
|
|
153
|
-
"""计算两个日期之间的天数差"""
|
|
154
|
-
return abs((dt2 - dt1).days)
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def is_leap_year(year):
|
|
158
|
-
"""判断是否是闰年"""
|
|
159
|
-
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
def parse_relative_time(time_str: str) -> str:
|
|
163
|
-
"""
|
|
164
|
-
解析相对时间字符串(如 "3分钟前"、"昨天")为 datetime 对象。
|
|
165
|
-
"""
|
|
166
|
-
dt = dateparser.parse(time_str)
|
|
167
|
-
return dt.isoformat()
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if __name__ == '__main__':
|
|
171
|
-
print(normalize_time(parse_relative_time("30分钟前")))
|
|
172
|
-
print(parse_relative_time("昨天"))
|
|
173
|
-
print(parse_relative_time("10小时前"))
|
|
174
|
-
print(parse_relative_time("1个月前"))
|
|
175
|
-
print(parse_relative_time("10天前"))
|
|
176
|
-
print(parse_relative_time("2024年1月1日"))
|
|
177
|
-
print(parse_relative_time('2025年5月30日'))
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding:UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
# @Time : 2025-05-17 10:20
|
|
5
|
+
# @Author : crawl-coder
|
|
6
|
+
# @Desc : 时间工具
|
|
7
|
+
"""
|
|
8
|
+
import dateparser
|
|
9
|
+
from typing import Optional, Union
|
|
10
|
+
from datetime import datetime, timedelta
|
|
11
|
+
from dateutil.relativedelta import relativedelta
|
|
12
|
+
|
|
13
|
+
# 常见时间格式列表
|
|
14
|
+
COMMON_FORMATS = [
|
|
15
|
+
"%Y-%m-%d %H:%M:%S",
|
|
16
|
+
"%Y/%m/%d %H:%M:%S",
|
|
17
|
+
"%d-%m-%Y %H:%M:%S",
|
|
18
|
+
"%d/%m/%Y %H:%M:%S",
|
|
19
|
+
"%Y-%m-%d",
|
|
20
|
+
"%Y/%m/%d",
|
|
21
|
+
"%d-%m-%Y",
|
|
22
|
+
"%d/%m/%Y",
|
|
23
|
+
"%b %d, %Y", # Jan 01, 2023
|
|
24
|
+
"%B %d, %Y", # January 01, 2023
|
|
25
|
+
"%Y年%m月%d日", # 2023年01月01日
|
|
26
|
+
"%Y年%m月%d日 %H时%M分%S秒", # 2023年01月01日 12时30分45秒
|
|
27
|
+
"%a %b %d %H:%M:%S %Y", # Wed Jan 01 12:00:00 2020
|
|
28
|
+
"%a, %d %b %Y %H:%M:%S", # Wed, 01 Jan 2020 12:00:00
|
|
29
|
+
"%Y-%m-%dT%H:%M:%S.%f", # ✅ 新增:ISO 8601 格式(带毫秒)
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def normalize_time(time_str: str) -> Optional[datetime]:
|
|
34
|
+
"""
|
|
35
|
+
尝试使用常见格式解析时间字符串。
|
|
36
|
+
|
|
37
|
+
:param time_str: 时间字符串(如 "2023-01-01 12:00:00")
|
|
38
|
+
:return: 解析成功返回 datetime 对象,失败返回 None 或抛出异常(可选)
|
|
39
|
+
"""
|
|
40
|
+
for fmt in COMMON_FORMATS:
|
|
41
|
+
try:
|
|
42
|
+
return datetime.strptime(time_str, fmt)
|
|
43
|
+
except ValueError:
|
|
44
|
+
continue
|
|
45
|
+
raise ValueError(f"无法解析时间字符串:{time_str}")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_current_time(fmt: str = '%Y-%m-%d %H:%M:%S'):
|
|
49
|
+
"""
|
|
50
|
+
获取当前时间,根据是否传入格式化参数决定返回类型
|
|
51
|
+
:param fmt: 格式化字符串,如 "%Y-%m-%d %H:%M:%S"
|
|
52
|
+
:return: datetime 或 str
|
|
53
|
+
"""
|
|
54
|
+
dt = datetime.now()
|
|
55
|
+
if fmt is not None:
|
|
56
|
+
return dt.strftime(fmt)
|
|
57
|
+
return dt
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def time_diff_seconds(start_time: str, end_time: str, fmt: str = '%Y-%m-%d %H:%M:%S'):
|
|
61
|
+
"""
|
|
62
|
+
计算两个时间字符串之间的秒数差。
|
|
63
|
+
|
|
64
|
+
:param start_time: 起始时间字符串
|
|
65
|
+
:param end_time: 结束时间字符串
|
|
66
|
+
:param fmt: 时间格式,默认为 '%Y-%m-%d %H:%M:%S'
|
|
67
|
+
:return: 秒数差(总是正整数)
|
|
68
|
+
"""
|
|
69
|
+
start = datetime.strptime(start_time, fmt)
|
|
70
|
+
end = datetime.strptime(end_time, fmt)
|
|
71
|
+
delta = end - start
|
|
72
|
+
return int(delta.total_seconds())
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
TimeType = Union[str, datetime]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def time_diff(start: TimeType, end: TimeType, fmt: str = None, unit='seconds', auto_parse=True) -> Optional[int]:
|
|
79
|
+
"""
|
|
80
|
+
计算两个时间之间的差值(支持字符串或 datetime)。
|
|
81
|
+
|
|
82
|
+
:param start: 起始时间(字符串或 datetime)
|
|
83
|
+
:param end: 结束时间(字符串或 datetime)
|
|
84
|
+
:param fmt: 时间格式(如果传入字符串且 auto_parse=False 时需要)
|
|
85
|
+
:param unit: 单位(seconds, minutes, hours, days)
|
|
86
|
+
:param auto_parse: 是否自动尝试解析任意格式的字符串(推荐开启)
|
|
87
|
+
:return: 差值整数(根据 unit 返回),失败返回 None
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def ensure_datetime(t):
|
|
91
|
+
if isinstance(t, datetime):
|
|
92
|
+
return t
|
|
93
|
+
elif isinstance(t, str):
|
|
94
|
+
if auto_parse:
|
|
95
|
+
parsed = normalize_time(t)
|
|
96
|
+
if parsed:
|
|
97
|
+
return parsed
|
|
98
|
+
if fmt:
|
|
99
|
+
return datetime.strptime(t, fmt)
|
|
100
|
+
raise ValueError("字符串时间未提供格式,或无法自动解析")
|
|
101
|
+
else:
|
|
102
|
+
raise TypeError(f"不支持的时间类型: {type(t)}")
|
|
103
|
+
|
|
104
|
+
start_dt = ensure_datetime(start)
|
|
105
|
+
end_dt = ensure_datetime(end)
|
|
106
|
+
|
|
107
|
+
delta = (end_dt - start_dt)
|
|
108
|
+
abs_seconds = int(abs(delta.total_seconds()))
|
|
109
|
+
|
|
110
|
+
if unit == 'seconds':
|
|
111
|
+
return abs_seconds
|
|
112
|
+
elif unit == 'minutes':
|
|
113
|
+
return abs_seconds // 60
|
|
114
|
+
elif unit == 'hours':
|
|
115
|
+
return abs_seconds // 3600
|
|
116
|
+
elif unit == 'days':
|
|
117
|
+
return abs_seconds // 86400
|
|
118
|
+
else:
|
|
119
|
+
raise ValueError(f"Unsupported unit: {unit}")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def format_datetime(dt, fmt="%Y-%m-%d %H:%M:%S"):
|
|
123
|
+
"""格式化时间"""
|
|
124
|
+
return dt.strftime(fmt)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def parse_datetime(s, fmt="%Y-%m-%d %H:%M:%S"):
|
|
128
|
+
"""将字符串解析为 datetime 对象"""
|
|
129
|
+
return datetime.strptime(s, fmt)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def datetime_to_timestamp(dt):
|
|
133
|
+
"""将 datetime 转换为时间戳"""
|
|
134
|
+
return dt.timestamp()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def timestamp_to_datetime(ts):
|
|
138
|
+
"""将时间戳转换为 datetime"""
|
|
139
|
+
return datetime.fromtimestamp(ts)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def add_days(dt, days=0):
|
|
143
|
+
"""日期加减(天)"""
|
|
144
|
+
return dt + timedelta(days=days)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def add_months(dt, months=0):
|
|
148
|
+
"""日期加减(月)"""
|
|
149
|
+
return dt + relativedelta(months=months)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def days_between(dt1, dt2):
|
|
153
|
+
"""计算两个日期之间的天数差"""
|
|
154
|
+
return abs((dt2 - dt1).days)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def is_leap_year(year):
|
|
158
|
+
"""判断是否是闰年"""
|
|
159
|
+
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def parse_relative_time(time_str: str) -> str:
|
|
163
|
+
"""
|
|
164
|
+
解析相对时间字符串(如 "3分钟前"、"昨天")为 datetime 对象。
|
|
165
|
+
"""
|
|
166
|
+
dt = dateparser.parse(time_str)
|
|
167
|
+
return dt.isoformat()
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
if __name__ == '__main__':
|
|
171
|
+
print(normalize_time(parse_relative_time("30分钟前")))
|
|
172
|
+
print(parse_relative_time("昨天"))
|
|
173
|
+
print(parse_relative_time("10小时前"))
|
|
174
|
+
print(parse_relative_time("1个月前"))
|
|
175
|
+
print(parse_relative_time("10天前"))
|
|
176
|
+
print(parse_relative_time("2024年1月1日"))
|
|
177
|
+
print(parse_relative_time('2025年5月30日'))
|
crawlo/utils/func_tools.py
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
# -*- coding: UTF-8 -*-
|
|
2
|
-
from typing import Union, AsyncGenerator, Generator
|
|
3
|
-
from inspect import isgenerator, isasyncgen
|
|
4
|
-
from crawlo import Response, Request, Item
|
|
5
|
-
from crawlo.exceptions import TransformTypeError
|
|
6
|
-
|
|
7
|
-
T = Union[Request, Item]
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
async def transform(
|
|
11
|
-
func: Union[Generator[T, None, None], AsyncGenerator[T, None]],
|
|
12
|
-
response: Response
|
|
13
|
-
) -> AsyncGenerator[Union[T, Exception], None]:
|
|
14
|
-
"""
|
|
15
|
-
转换回调函数的输出为统一异步生成器
|
|
16
|
-
|
|
17
|
-
Args:
|
|
18
|
-
func: 同步或异步生成器函数
|
|
19
|
-
response: 当前响应对象
|
|
20
|
-
|
|
21
|
-
Yields:
|
|
22
|
-
Union[T, Exception]: 生成请求/Item或异常对象
|
|
23
|
-
|
|
24
|
-
Raises:
|
|
25
|
-
TransformTypeError: 当输入类型不符合要求时
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
def _set_meta(obj: T) -> T:
|
|
29
|
-
"""统一设置请求的depth元数据"""
|
|
30
|
-
if isinstance(obj, Request):
|
|
31
|
-
obj.meta.setdefault('depth', response.meta.get('depth', 0))
|
|
32
|
-
return obj
|
|
33
|
-
|
|
34
|
-
# 类型检查前置
|
|
35
|
-
if not (isgenerator(func) or isasyncgen(func)):
|
|
36
|
-
raise TransformTypeError(
|
|
37
|
-
f'Callback must return generator or async generator, got {type(func).__name__}'
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
try:
|
|
41
|
-
if isgenerator(func):
|
|
42
|
-
# 同步生成器处理
|
|
43
|
-
for item in func:
|
|
44
|
-
yield _set_meta(item)
|
|
45
|
-
else:
|
|
46
|
-
# 异步生成器处理
|
|
47
|
-
async for item in func:
|
|
48
|
-
yield _set_meta(item)
|
|
49
|
-
|
|
50
|
-
except Exception as e:
|
|
51
|
-
yield e
|
|
52
|
-
|
|
53
|
-
# #!/usr/bin/python
|
|
54
|
-
# # -*- coding:UTF-8 -*-
|
|
55
|
-
# from typing import Callable, Union
|
|
56
|
-
# from inspect import isgenerator, isasyncgen
|
|
57
|
-
# from crawlo import Response, Request, Item
|
|
58
|
-
# from crawlo.exceptions import TransformTypeError
|
|
59
|
-
#
|
|
60
|
-
#
|
|
61
|
-
# T = Union[Request, Item]
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
# async def transform(func: Callable, response: Response):
|
|
65
|
-
# def set_request(t: T) -> T:
|
|
66
|
-
# if isinstance(t, Request):
|
|
67
|
-
# t.meta['depth'] = response.meta['depth']
|
|
68
|
-
# return t
|
|
69
|
-
# try:
|
|
70
|
-
# if isgenerator(func):
|
|
71
|
-
# for f in func:
|
|
72
|
-
# yield set_request(f)
|
|
73
|
-
# elif isasyncgen(func):
|
|
74
|
-
# async for f in func:
|
|
75
|
-
# yield set_request(f)
|
|
76
|
-
# else:
|
|
77
|
-
# raise TransformTypeError(
|
|
78
|
-
# f'callback return type error: {type(func)} must be `generator` or `async generator`'
|
|
79
|
-
# )
|
|
80
|
-
# except Exception as exp:
|
|
81
|
-
# yield exp
|
|
82
|
-
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
from typing import Union, AsyncGenerator, Generator
|
|
3
|
+
from inspect import isgenerator, isasyncgen
|
|
4
|
+
from crawlo import Response, Request, Item
|
|
5
|
+
from crawlo.exceptions import TransformTypeError
|
|
6
|
+
|
|
7
|
+
T = Union[Request, Item]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def transform(
|
|
11
|
+
func: Union[Generator[T, None, None], AsyncGenerator[T, None]],
|
|
12
|
+
response: Response
|
|
13
|
+
) -> AsyncGenerator[Union[T, Exception], None]:
|
|
14
|
+
"""
|
|
15
|
+
转换回调函数的输出为统一异步生成器
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
func: 同步或异步生成器函数
|
|
19
|
+
response: 当前响应对象
|
|
20
|
+
|
|
21
|
+
Yields:
|
|
22
|
+
Union[T, Exception]: 生成请求/Item或异常对象
|
|
23
|
+
|
|
24
|
+
Raises:
|
|
25
|
+
TransformTypeError: 当输入类型不符合要求时
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def _set_meta(obj: T) -> T:
|
|
29
|
+
"""统一设置请求的depth元数据"""
|
|
30
|
+
if isinstance(obj, Request):
|
|
31
|
+
obj.meta.setdefault('depth', response.meta.get('depth', 0))
|
|
32
|
+
return obj
|
|
33
|
+
|
|
34
|
+
# 类型检查前置
|
|
35
|
+
if not (isgenerator(func) or isasyncgen(func)):
|
|
36
|
+
raise TransformTypeError(
|
|
37
|
+
f'Callback must return generator or async generator, got {type(func).__name__}'
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
if isgenerator(func):
|
|
42
|
+
# 同步生成器处理
|
|
43
|
+
for item in func:
|
|
44
|
+
yield _set_meta(item)
|
|
45
|
+
else:
|
|
46
|
+
# 异步生成器处理
|
|
47
|
+
async for item in func:
|
|
48
|
+
yield _set_meta(item)
|
|
49
|
+
|
|
50
|
+
except Exception as e:
|
|
51
|
+
yield e
|
|
52
|
+
|
|
53
|
+
# #!/usr/bin/python
|
|
54
|
+
# # -*- coding:UTF-8 -*-
|
|
55
|
+
# from typing import Callable, Union
|
|
56
|
+
# from inspect import isgenerator, isasyncgen
|
|
57
|
+
# from crawlo import Response, Request, Item
|
|
58
|
+
# from crawlo.exceptions import TransformTypeError
|
|
59
|
+
#
|
|
60
|
+
#
|
|
61
|
+
# T = Union[Request, Item]
|
|
62
|
+
#
|
|
63
|
+
#
|
|
64
|
+
# async def transform(func: Callable, response: Response):
|
|
65
|
+
# def set_request(t: T) -> T:
|
|
66
|
+
# if isinstance(t, Request):
|
|
67
|
+
# t.meta['depth'] = response.meta['depth']
|
|
68
|
+
# return t
|
|
69
|
+
# try:
|
|
70
|
+
# if isgenerator(func):
|
|
71
|
+
# for f in func:
|
|
72
|
+
# yield set_request(f)
|
|
73
|
+
# elif isasyncgen(func):
|
|
74
|
+
# async for f in func:
|
|
75
|
+
# yield set_request(f)
|
|
76
|
+
# else:
|
|
77
|
+
# raise TransformTypeError(
|
|
78
|
+
# f'callback return type error: {type(func)} must be `generator` or `async generator`'
|
|
79
|
+
# )
|
|
80
|
+
# except Exception as exp:
|
|
81
|
+
# yield exp
|
|
82
|
+
|
crawlo/utils/log.py
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
#!/usr/bin/python
|
|
2
|
-
# -*- coding:UTF-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
# @Time : 2024-04-11 09:03
|
|
5
|
-
# @Author : oscar
|
|
6
|
-
# @Desc : None
|
|
7
|
-
"""
|
|
8
|
-
from logging import Formatter, StreamHandler, Logger, INFO
|
|
9
|
-
|
|
10
|
-
LOG_FORMAT = '%(asctime)s - [%(name)s] - %(levelname)s: %(message)s'
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class LoggerManager(object):
|
|
14
|
-
logger_cache = {}
|
|
15
|
-
|
|
16
|
-
def __init__(self):
|
|
17
|
-
pass
|
|
18
|
-
|
|
19
|
-
@classmethod
|
|
20
|
-
def get_logger(cls, name: str = 'default', level=None, log_format: str = LOG_FORMAT):
|
|
21
|
-
key = (name, level)
|
|
22
|
-
|
|
23
|
-
def gen_logger():
|
|
24
|
-
log_formatter = Formatter(log_format)
|
|
25
|
-
handler = StreamHandler()
|
|
26
|
-
handler.setFormatter(log_formatter)
|
|
27
|
-
handler.setLevel(level or INFO)
|
|
28
|
-
|
|
29
|
-
_logger = Logger(name=name)
|
|
30
|
-
_logger.addHandler(handler)
|
|
31
|
-
_logger.setLevel(level or INFO)
|
|
32
|
-
cls.logger_cache[key] = _logger
|
|
33
|
-
return _logger
|
|
34
|
-
|
|
35
|
-
return cls.logger_cache.get(key, None) or gen_logger()
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
get_logger = LoggerManager.get_logger
|
|
39
|
-
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding:UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
# @Time : 2024-04-11 09:03
|
|
5
|
+
# @Author : oscar
|
|
6
|
+
# @Desc : None
|
|
7
|
+
"""
|
|
8
|
+
from logging import Formatter, StreamHandler, Logger, INFO
|
|
9
|
+
|
|
10
|
+
LOG_FORMAT = '%(asctime)s - [%(name)s] - %(levelname)s: %(message)s'
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class LoggerManager(object):
|
|
14
|
+
logger_cache = {}
|
|
15
|
+
|
|
16
|
+
def __init__(self):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
@classmethod
|
|
20
|
+
def get_logger(cls, name: str = 'default', level=None, log_format: str = LOG_FORMAT):
|
|
21
|
+
key = (name, level)
|
|
22
|
+
|
|
23
|
+
def gen_logger():
|
|
24
|
+
log_formatter = Formatter(log_format)
|
|
25
|
+
handler = StreamHandler()
|
|
26
|
+
handler.setFormatter(log_formatter)
|
|
27
|
+
handler.setLevel(level or INFO)
|
|
28
|
+
|
|
29
|
+
_logger = Logger(name=name)
|
|
30
|
+
_logger.addHandler(handler)
|
|
31
|
+
_logger.setLevel(level or INFO)
|
|
32
|
+
cls.logger_cache[key] = _logger
|
|
33
|
+
return _logger
|
|
34
|
+
|
|
35
|
+
return cls.logger_cache.get(key, None) or gen_logger()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
get_logger = LoggerManager.get_logger
|
|
39
|
+
|