crawlo 1.0.2__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.

Files changed (79) hide show
  1. crawlo/__init__.py +9 -6
  2. crawlo/__version__.py +1 -2
  3. crawlo/core/__init__.py +2 -2
  4. crawlo/core/engine.py +158 -158
  5. crawlo/core/processor.py +40 -40
  6. crawlo/core/scheduler.py +57 -59
  7. crawlo/crawler.py +242 -222
  8. crawlo/downloader/__init__.py +78 -78
  9. crawlo/downloader/aiohttp_downloader.py +259 -96
  10. crawlo/downloader/httpx_downloader.py +187 -48
  11. crawlo/downloader/playwright_downloader.py +160 -160
  12. crawlo/event.py +11 -11
  13. crawlo/exceptions.py +64 -64
  14. crawlo/extension/__init__.py +31 -31
  15. crawlo/extension/log_interval.py +49 -49
  16. crawlo/extension/log_stats.py +44 -44
  17. crawlo/filters/__init__.py +37 -37
  18. crawlo/filters/aioredis_filter.py +157 -129
  19. crawlo/filters/memory_filter.py +202 -203
  20. crawlo/filters/redis_filter.py +119 -119
  21. crawlo/items/__init__.py +62 -62
  22. crawlo/items/items.py +118 -118
  23. crawlo/middleware/__init__.py +21 -21
  24. crawlo/middleware/default_header.py +32 -32
  25. crawlo/middleware/download_delay.py +28 -28
  26. crawlo/middleware/middleware_manager.py +140 -140
  27. crawlo/middleware/request_ignore.py +30 -30
  28. crawlo/middleware/response_code.py +18 -18
  29. crawlo/middleware/response_filter.py +26 -26
  30. crawlo/middleware/retry.py +90 -90
  31. crawlo/network/__init__.py +7 -7
  32. crawlo/network/request.py +204 -233
  33. crawlo/network/response.py +166 -162
  34. crawlo/pipelines/__init__.py +13 -13
  35. crawlo/pipelines/console_pipeline.py +39 -39
  36. crawlo/pipelines/mongo_pipeline.py +116 -116
  37. crawlo/pipelines/mysql_batch_pipline.py +133 -133
  38. crawlo/pipelines/mysql_pipeline.py +195 -195
  39. crawlo/pipelines/pipeline_manager.py +56 -56
  40. crawlo/settings/__init__.py +7 -7
  41. crawlo/settings/default_settings.py +93 -89
  42. crawlo/settings/setting_manager.py +99 -99
  43. crawlo/spider/__init__.py +36 -36
  44. crawlo/stats_collector.py +59 -47
  45. crawlo/subscriber.py +106 -106
  46. crawlo/task_manager.py +27 -27
  47. crawlo/templates/item_template.tmpl +21 -21
  48. crawlo/templates/project_template/main.py +32 -32
  49. crawlo/templates/project_template/setting.py +189 -189
  50. crawlo/templates/spider_template.tmpl +30 -30
  51. crawlo/utils/__init__.py +7 -7
  52. crawlo/utils/concurrency_manager.py +124 -124
  53. crawlo/utils/date_tools.py +177 -177
  54. crawlo/utils/func_tools.py +82 -82
  55. crawlo/utils/log.py +39 -39
  56. crawlo/utils/pqueue.py +173 -173
  57. crawlo/utils/project.py +59 -59
  58. crawlo/utils/request.py +122 -85
  59. crawlo/utils/system.py +11 -11
  60. crawlo/utils/tools.py +302 -302
  61. crawlo/utils/url.py +39 -39
  62. {crawlo-1.0.2.dist-info → crawlo-1.0.3.dist-info}/METADATA +48 -48
  63. crawlo-1.0.3.dist-info/RECORD +80 -0
  64. {crawlo-1.0.2.dist-info → crawlo-1.0.3.dist-info}/top_level.txt +1 -0
  65. tests/__init__.py +7 -0
  66. tests/baidu_spider/__init__.py +7 -0
  67. tests/baidu_spider/demo.py +94 -0
  68. tests/baidu_spider/items.py +25 -0
  69. tests/baidu_spider/middleware.py +49 -0
  70. tests/baidu_spider/pipeline.py +55 -0
  71. tests/baidu_spider/request_fingerprints.txt +9 -0
  72. tests/baidu_spider/run.py +27 -0
  73. tests/baidu_spider/settings.py +78 -0
  74. tests/baidu_spider/spiders/__init__.py +7 -0
  75. tests/baidu_spider/spiders/bai_du.py +61 -0
  76. tests/baidu_spider/spiders/sina.py +79 -0
  77. crawlo-1.0.2.dist-info/RECORD +0 -68
  78. {crawlo-1.0.2.dist-info → crawlo-1.0.3.dist-info}/WHEEL +0 -0
  79. {crawlo-1.0.2.dist-info → crawlo-1.0.3.dist-info}/entry_points.txt +0 -0
crawlo/subscriber.py CHANGED
@@ -1,106 +1,106 @@
1
- #!/usr/bin/python
2
- # -*- coding:UTF-8 -*-
3
- import asyncio
4
- from collections import defaultdict
5
- from inspect import iscoroutinefunction
6
- from typing import Dict, Set, Callable, Coroutine, Any, TypeAlias, List
7
-
8
-
9
- class ReceiverTypeError(TypeError):
10
- """当订阅的接收者不是一个协程函数时抛出。"""
11
- pass
12
-
13
-
14
- ReceiverCoroutine: TypeAlias = Callable[..., Coroutine[Any, Any, Any]]
15
-
16
-
17
- class Subscriber:
18
- """
19
- 一个支持异步协程的发布/订阅(Pub/Sub)模式实现。
20
-
21
- 这个类允许你注册(订阅)协程函数来监听特定事件,并在事件发生时
22
- 以并发的方式异步地通知所有订阅者。
23
- """
24
-
25
- def __init__(self):
26
- """初始化一个空的订阅者字典。"""
27
- self._subscribers: Dict[str, Set[ReceiverCoroutine]] = defaultdict(set)
28
-
29
- def subscribe(self, receiver: ReceiverCoroutine, *, event: str) -> None:
30
- """
31
- 订阅一个事件。
32
-
33
- Args:
34
- receiver: 一个协程函数 (例如 async def my_func(...))。
35
- event: 要订阅的事件名称。
36
-
37
- Raises:
38
- ReceiverTypeError: 如果提供的 `receiver` 不是一个协程函数。
39
- """
40
- if not iscoroutinefunction(receiver):
41
- raise ReceiverTypeError(f"接收者 '{receiver.__qualname__}' 必须是一个协程函数。")
42
- self._subscribers[event].add(receiver)
43
-
44
- def unsubscribe(self, receiver: ReceiverCoroutine, *, event: str) -> None:
45
- """
46
- 取消订阅一个事件。
47
-
48
- 如果事件或接收者不存在,将静默处理。
49
-
50
- Args:
51
- receiver: 要取消订阅的协程函数。
52
- event: 事件名称。
53
- """
54
- if event in self._subscribers:
55
- self._subscribers[event].discard(receiver)
56
-
57
- async def notify(self, event: str, *args, **kwargs) -> List[Any]:
58
- """
59
- 异步地、并发地通知所有订阅了该事件的接收者。
60
-
61
- 此方法会等待所有订阅者任务完成后再返回,并收集所有结果或异常。
62
-
63
- Args:
64
- event: 要触发的事件名称。
65
- *args: 传递给接收者的位置参数。
66
- **kwargs: 传递给接收者的关键字参数。
67
-
68
- Returns:
69
- 一个列表,包含每个订阅者任务的返回结果或在执行期间捕获的异常。
70
- """
71
- receivers = self._subscribers.get(event, set())
72
- if not receivers:
73
- return []
74
-
75
- tasks = [asyncio.create_task(receiver(*args, **kwargs)) for receiver in receivers]
76
-
77
- # 并发执行所有任务并返回结果列表(包括异常)
78
- return await asyncio.gather(*tasks, return_exceptions=True)
79
-
80
- # #!/usr/bin/python
81
- # # -*- coding:UTF-8 -*-
82
- # import asyncio
83
- # from collections import defaultdict
84
- # from inspect import iscoroutinefunction
85
- # from typing import Dict, Set, Callable, Coroutine
86
- #
87
- # from crawlo.exceptions import ReceiverTypeError
88
- #
89
- #
90
- # class Subscriber:
91
- #
92
- # def __init__(self):
93
- # self._subscribers: Dict[str, Set[Callable[..., Coroutine]]] = defaultdict(set)
94
- #
95
- # def subscribe(self, receiver: Callable[..., Coroutine], *, event: str) -> None:
96
- # if not iscoroutinefunction(receiver):
97
- # raise ReceiverTypeError(f"{receiver.__qualname__} must be a coroutine function")
98
- # self._subscribers[event].add(receiver)
99
- #
100
- # def unsubscribe(self, receiver: Callable[..., Coroutine], *, event: str) -> None:
101
- # self._subscribers[event].discard(receiver)
102
- #
103
- # async def notify(self, event: str, *args, **kwargs) -> None:
104
- # for receiver in self._subscribers[event]:
105
- # # 不能 await
106
- # asyncio.create_task(receiver(*args, **kwargs))
1
+ #!/usr/bin/python
2
+ # -*- coding:UTF-8 -*-
3
+ import asyncio
4
+ from collections import defaultdict
5
+ from inspect import iscoroutinefunction
6
+ from typing import Dict, Set, Callable, Coroutine, Any, TypeAlias, List
7
+
8
+
9
+ class ReceiverTypeError(TypeError):
10
+ """当订阅的接收者不是一个协程函数时抛出。"""
11
+ pass
12
+
13
+
14
+ ReceiverCoroutine: TypeAlias = Callable[..., Coroutine[Any, Any, Any]]
15
+
16
+
17
+ class Subscriber:
18
+ """
19
+ 一个支持异步协程的发布/订阅(Pub/Sub)模式实现。
20
+
21
+ 这个类允许你注册(订阅)协程函数来监听特定事件,并在事件发生时
22
+ 以并发的方式异步地通知所有订阅者。
23
+ """
24
+
25
+ def __init__(self):
26
+ """初始化一个空的订阅者字典。"""
27
+ self._subscribers: Dict[str, Set[ReceiverCoroutine]] = defaultdict(set)
28
+
29
+ def subscribe(self, receiver: ReceiverCoroutine, *, event: str) -> None:
30
+ """
31
+ 订阅一个事件。
32
+
33
+ Args:
34
+ receiver: 一个协程函数 (例如 async def my_func(...))。
35
+ event: 要订阅的事件名称。
36
+
37
+ Raises:
38
+ ReceiverTypeError: 如果提供的 `receiver` 不是一个协程函数。
39
+ """
40
+ if not iscoroutinefunction(receiver):
41
+ raise ReceiverTypeError(f"接收者 '{receiver.__qualname__}' 必须是一个协程函数。")
42
+ self._subscribers[event].add(receiver)
43
+
44
+ def unsubscribe(self, receiver: ReceiverCoroutine, *, event: str) -> None:
45
+ """
46
+ 取消订阅一个事件。
47
+
48
+ 如果事件或接收者不存在,将静默处理。
49
+
50
+ Args:
51
+ receiver: 要取消订阅的协程函数。
52
+ event: 事件名称。
53
+ """
54
+ if event in self._subscribers:
55
+ self._subscribers[event].discard(receiver)
56
+
57
+ async def notify(self, event: str, *args, **kwargs) -> List[Any]:
58
+ """
59
+ 异步地、并发地通知所有订阅了该事件的接收者。
60
+
61
+ 此方法会等待所有订阅者任务完成后再返回,并收集所有结果或异常。
62
+
63
+ Args:
64
+ event: 要触发的事件名称。
65
+ *args: 传递给接收者的位置参数。
66
+ **kwargs: 传递给接收者的关键字参数。
67
+
68
+ Returns:
69
+ 一个列表,包含每个订阅者任务的返回结果或在执行期间捕获的异常。
70
+ """
71
+ receivers = self._subscribers.get(event, set())
72
+ if not receivers:
73
+ return []
74
+
75
+ tasks = [asyncio.create_task(receiver(*args, **kwargs)) for receiver in receivers]
76
+
77
+ # 并发执行所有任务并返回结果列表(包括异常)
78
+ return await asyncio.gather(*tasks, return_exceptions=True)
79
+
80
+ # #!/usr/bin/python
81
+ # # -*- coding:UTF-8 -*-
82
+ # import asyncio
83
+ # from collections import defaultdict
84
+ # from inspect import iscoroutinefunction
85
+ # from typing import Dict, Set, Callable, Coroutine
86
+ #
87
+ # from crawlo.exceptions import ReceiverTypeError
88
+ #
89
+ #
90
+ # class Subscriber:
91
+ #
92
+ # def __init__(self):
93
+ # self._subscribers: Dict[str, Set[Callable[..., Coroutine]]] = defaultdict(set)
94
+ #
95
+ # def subscribe(self, receiver: Callable[..., Coroutine], *, event: str) -> None:
96
+ # if not iscoroutinefunction(receiver):
97
+ # raise ReceiverTypeError(f"{receiver.__qualname__} must be a coroutine function")
98
+ # self._subscribers[event].add(receiver)
99
+ #
100
+ # def unsubscribe(self, receiver: Callable[..., Coroutine], *, event: str) -> None:
101
+ # self._subscribers[event].discard(receiver)
102
+ #
103
+ # async def notify(self, event: str, *args, **kwargs) -> None:
104
+ # for receiver in self._subscribers[event]:
105
+ # # 不能 await
106
+ # asyncio.create_task(receiver(*args, **kwargs))
crawlo/task_manager.py CHANGED
@@ -1,27 +1,27 @@
1
- #!/usr/bin/python
2
- # -*- coding:UTF-8 -*-
3
- import asyncio
4
- from asyncio import Task, Future, Semaphore
5
- from typing import Set, Final
6
-
7
-
8
- class TaskManager:
9
-
10
- def __init__(self, total_concurrency: int = 8):
11
- self.current_task: Final[Set] = set()
12
- self.semaphore: Semaphore = Semaphore(total_concurrency)
13
-
14
- def create_task(self, coroutine) -> Task:
15
- task = asyncio.create_task(coroutine)
16
- self.current_task.add(task)
17
-
18
- def done_callback(_future: Future) -> None:
19
- self.current_task.remove(task)
20
- self.semaphore.release()
21
-
22
- task.add_done_callback(done_callback)
23
-
24
- return task
25
-
26
- def all_done(self) -> bool:
27
- return len(self.current_task) == 0
1
+ #!/usr/bin/python
2
+ # -*- coding:UTF-8 -*-
3
+ import asyncio
4
+ from asyncio import Task, Future, Semaphore
5
+ from typing import Set, Final
6
+
7
+
8
+ class TaskManager:
9
+
10
+ def __init__(self, total_concurrency: int = 8):
11
+ self.current_task: Final[Set] = set()
12
+ self.semaphore: Semaphore = Semaphore(total_concurrency)
13
+
14
+ def create_task(self, coroutine) -> Task:
15
+ task = asyncio.create_task(coroutine)
16
+ self.current_task.add(task)
17
+
18
+ def done_callback(_future: Future) -> None:
19
+ self.current_task.remove(task)
20
+ self.semaphore.release()
21
+
22
+ task.add_done_callback(done_callback)
23
+
24
+ return task
25
+
26
+ def all_done(self) -> bool:
27
+ return len(self.current_task) == 0
@@ -1,22 +1,22 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Created on {DATE}
4
- ---------
5
- @summary:
6
- ---------
7
- @author: {USER}
8
- """
9
-
10
- from crawlo import Item
11
-
12
-
13
- class ${item_name}Item(Item):
14
- """
15
- This class was generated by feapder
16
- command: feapder create -i ${command}
17
- """
18
-
19
- __table_name__ = "${table_name}"
20
-
21
- def __init__(self, *args, **kwargs):
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on {DATE}
4
+ ---------
5
+ @summary:
6
+ ---------
7
+ @author: {USER}
8
+ """
9
+
10
+ from crawlo import Item
11
+
12
+
13
+ class ${item_name}Item(Item):
14
+ """
15
+ This class was generated by feapder
16
+ command: feapder create -i ${command}
17
+ """
18
+
19
+ __table_name__ = "${table_name}"
20
+
21
+ def __init__(self, *args, **kwargs):
22
22
  ${propertys}
@@ -1,33 +1,33 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Created on {DATE}
4
- ---------
5
- @summary: 爬虫入口
6
- ---------
7
- @author: {USER}
8
- """
9
-
10
- from crawlo import ArgumentParser
11
-
12
- from spiders import *
13
-
14
-
15
-
16
- def crawl_xxx():
17
- """
18
- Spider爬虫
19
- """
20
- spider = xxx.XXXSpider(redis_key="xxx:xxx")
21
- spider.start()
22
-
23
-
24
-
25
- if __name__ == "__main__":
26
- parser = ArgumentParser(description="xxx爬虫")
27
-
28
- parser.add_argument(
29
- "--crawl_xxx", action="store_true", help="xxx爬虫", function=crawl_xxx
30
- )
31
- parser.start()
32
-
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on {DATE}
4
+ ---------
5
+ @summary: 爬虫入口
6
+ ---------
7
+ @author: {USER}
8
+ """
9
+
10
+ from crawlo import ArgumentParser
11
+
12
+ from spiders import *
13
+
14
+
15
+
16
+ def crawl_xxx():
17
+ """
18
+ Spider爬虫
19
+ """
20
+ spider = xxx.XXXSpider(redis_key="xxx:xxx")
21
+ spider.start()
22
+
23
+
24
+
25
+ if __name__ == "__main__":
26
+ parser = ArgumentParser(description="xxx爬虫")
27
+
28
+ parser.add_argument(
29
+ "--crawl_xxx", action="store_true", help="xxx爬虫", function=crawl_xxx
30
+ )
31
+ parser.start()
32
+
33
33
  # main.py作为爬虫启动的统一入口,提供命令行的方式启动多个爬虫