2017-01-06 8 views
0

パイプラインから返された値を取得しようとしています。私は降伏ジェネレータを使ってアイテムを生成しています。Scrapyパイプラインから返された値を取得する

これは私のコードです。

def get_or_create(model): 
    model_class = type(model) 
    created = False 

    try: 
     obj = model_class.objects.get(product_company=model.product_company, barcode=model.barcode) 
    except model_class.DoesNotExist: 
     created = True 
     obj = model # DjangoItem created a model for us. 
     obj.save() 
    return (obj, created) 


def update_model(destination, source, commit=True): 
    pk = destination.pk 
    source_dict = model_to_dict(source) 
    for (key, value) in source_dict.items(): 
     setattr(destination, key, value) 
    setattr(destination, 'pk', pk) 
    if commit: 
     destination.save() 
    return destination 
class ProductItemPipeline(object): 
    def process_item(self, item, spider): 

     if isinstance(item, ProductItem): 
      item_model = item.instance 
      model, created = get_or_create(item_model) 
      item['cover_photo'] = item['files'][0]['path'] 
      if created: 
       item.save() 
       for image in item['files']: 
        imageItem = ProductImageItem(image=image['path'], product=model) 
        imageItem.save() 
       for comment in item['comments']: 
        commentItem = CommentItem(comment=comment.comment, product=model) 
        commentItem.save() 
      return model 

これは私のスパイダーです。

item = ProductItem(name=name, price=price, barcode=barcode, file_urls=objectImages, product_url=response.url,product_company=company, comments = comments) 
     product = yield item 
     print type(product) 
     print "yield product" 

および製品タイプは、あなたのpython yieldを理解していない

答えて

0

nonetype戻っています。いくつかの関数の結果を返すには、のようにyieldを使用する必要があります。あなたは、だからあなたのコードが

item = ProductItem(name=name, price=price, barcode=barcode, file_urls=objectImages, product_url=response.url,product_company=company, comments = comments) 
self.logger.debug(type(product)) 
self.logger.debug('yield product') 
yield item 
ようになります。この article

に発電機に関する詳細な情報を見つけることができます

関連する問題