當前位置: 首頁>>代碼示例>>Python>>正文


Python Catalog.append_catalog方法代碼示例

本文整理匯總了Python中catalog.Catalog.append_catalog方法的典型用法代碼示例。如果您正苦於以下問題:Python Catalog.append_catalog方法的具體用法?Python Catalog.append_catalog怎麽用?Python Catalog.append_catalog使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在catalog.Catalog的用法示例。


在下文中一共展示了Catalog.append_catalog方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Extractor

# 需要導入模塊: from catalog import Catalog [as 別名]
# 或者: from catalog.Catalog import append_catalog [as 別名]
class Extractor(object):
    __metaclass__ = ABCMeta

    name = 'Course Extractor'

    def __init__(self, spider=None, input_directory='raw_output'):
        """

        :param spider: spider
        :type spider: CourseSpider
        :return:
        """
        if spider is None:
            self.input_directory = input_directory
            self.catalog = Catalog()
            self.name = self.input_directory
            self.timestamp = date.today()
        else:
            self.input_directory = spider.get_raw_output()
            self.catalog = Catalog()
            self.name = spider.get_name()
            self.timestamp = spider.get_timestamp()

    def set_input_directory(self, directory):
        # set the directory of raw html files
        self.input_directory = directory

    @abstractmethod
    def extract_each(self, filename):
        """

        :param filename:
        :return: extracted catalog
        :rtype: Catalog
        """
        pass

    def extract_all(self):
        file_list = [self.input_directory+'/' + f for f in os.listdir(self.input_directory)]

        num_files = len(file_list)

        print '[INFO] start extracting from %s' % self.name
        print '[INFO] total files: %d' % num_files

        for each in file_list:
            print '[INFO] processing file: %s, %d remaining' % (each, num_files)
            self.catalog.append_catalog(self.extract_each(each))
            num_files -= 1

        print '[SUMMARY] finished extraction'

    def to_csv(self):
        print '[INFO] saving catalog to file'
        self.catalog.to_csv(self.name+'__'+str(self.timestamp), sep=',')

    def print_result(self):
        print self.catalog
開發者ID:kwang-coursera,項目名稱:CatalogCrawler,代碼行數:60,代碼來源:extractor.py


注:本文中的catalog.Catalog.append_catalog方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。