Store

存储器贯穿在脚本执行的全过程,帮助你在脚本执行过程中收集和存储信息

实现Store

在使用Store前,你需要实现一个存储类,继承自StoreBase,里面定义你自己的存储变量。当你实现set方法时(也可以不实现),在脚本执行过程中,如果某个action方法的返回值不为空值,则会自动调用set方法,传入方法名以及对应的返回值。

import crawlipt as cpt
class Store(cpt.StoreBase):

    @check
    def __init__(self, is_replace: bool = False):
        """
        :param is_replace: need replace the value of method or not
        """
        self.is_replace = is_replace
        self.data = {}

    @check
    def set(self, method: str, value: Any) -> None:
        if method in self.data.keys():
            if self.is_replace:
                self.data[method] = value
            else:
                self.data[method].append(value)
            return
        if self.is_replace:
            self.data[method] = value
        else:
            self.data[method] = [value]

上述实现已在crawlipt中内置

内置Store对象

使用Store

Store需要在你自己定义的action或者condition中作为参数传入

最后更新于