heyudev 2 năm trước cách đây
mục cha
commit
871baebde9

+ 17 - 0
crawler/context.py

@@ -0,0 +1,17 @@
+from crawler.strategy import Strategy
+
+
+class Context:
+    def __init__(self, strategy: Strategy) -> None:
+        self._strategy = strategy
+
+    @property
+    def strategy(self) -> Strategy:
+        pass
+
+    @strategy.setter
+    def strategy(self, strategy: Strategy) -> None:
+        self._strategy = strategy
+
+    def crawl(self) -> None:
+        self._strategy.crawl()

+ 6 - 1
crawler/run.py

@@ -2,6 +2,8 @@ import logging
 import sys
 
 from crawler import config
+from crawler.context import Context
+from crawler.strategy import XiaoniangaoHomePageStrategy
 
 
 class Main:
@@ -15,7 +17,10 @@ class Main:
         print("Platform = " + config.PLATFORM['xiaoniangao'])
         print("Strategy = " + config.STRATEGY['home_page'])
         print("Env = " + config.ENV['test'])
-
+        # TODO 根据参数判断 具体的策略
+        strategy = XiaoniangaoHomePageStrategy()
+        context = Context(strategy)
+        context.strategy.crawl()
 
 
 if __name__ == '__main__':

+ 0 - 0
crawler/shipinhao/__init__.py


+ 28 - 0
crawler/strategy.py

@@ -0,0 +1,28 @@
+from abc import ABC, abstractmethod
+from typing import List
+
+
+# 策略
+class Strategy(ABC):
+
+    @abstractmethod
+    def crawl(self, data: List):
+        pass
+
+
+class XiaoniangaoHomePageStrategy(Strategy):
+    def crawl(self, data: List) -> List:
+        # 具体逻辑
+        return data
+
+
+class XiguaHomePageStrategy(Strategy):
+    def crawl(self, data: List) -> List:
+        # 具体逻辑
+        return data
+
+
+class ShipinhaoHomePageStrategy(Strategy):
+    def crawl(self, data: List) -> List:
+        # 具体逻辑
+        return data

+ 0 - 0
crawler/xiaoniangao/__init__.py