2013-10-29 82 views

回答

0

确定它可能在(子类)MongoDBPipeline管道中。

以下是未经测试,但一个选项是将self.collection更改为集合字典,将项目类型映射到Mongo集合。

class CustomMongoDBPipeleine(MongoDBPipeline): 

    def __init__(self, settings): 
    ... 
    mapping = { 
     PersonItem: 'persons', 
     BookItem: 'books', 
    } 
    self.collection = {} 
    for itype, collection_name in mapping.items(): 
     self.collection[itype] = database[collection_name] 

映射可能来自配置,并且使用项类型类名直接替代项类。

而且使用这样的:

insert_item(self, item, spider): 
     ... 
     self.collection.get(type(item)).insert(item, continue_on_error=True) 
     ... 
0

您还可以创建两个不同的管道对每一种类型:在settings.py

class PersonPipeline(object): 
    def __init__(self): 
     connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT']) 
     db = connection[settings['MONGODB_DB']] 
     self.collection = db[settings['MONGODB_PERSON_COLLECTION']] # use the person collection 

    def process_item(self, item, spider): 
     if not isinstance(item,PersonItem): 
      return item # return the item to let other pipeline to handle it 
     self.collection.insert(dict(item)) 

class BookPipeline(object): 
    def __init__(self): 
     connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT']) 
     db = connection[settings['MONGODB_DB']] 
     self.collection = db[settings['MONGODB_BOOK_COLLECTION']] # use the book collection 

    def process_item(self, item, spider): 
     if not isinstance(item,PersonItem): 
      return item # return the item to let other pipeline to handle it 
     self.collection.insert(dict(item)) 

:在pipelines.py

ITEM_PIPELINES = { # declare the handle sequence 
    'myproject.pipelines.PersonPipeline':100, 
    'myproject.pipelines.BookPipeline':200, 
} 
MONGODB_SERVER = "localhost" 
MONGODB_PORT = 27017 
MONGODB_DB = "MyDB" # db name 
MONGODB_PLACE_COLLECTION = "persons" # collection name 
MONGODB_LIST_COLLECTION = "books" 

当一件物品返回时,PersonPipeline无线我会先处理它。如果该项目不是PersonItem类型,则它将返回到下一个管道,在此情况下为BookPipeline

相关问题